php.variable.operators,forms, sessionphp

swamisheetal982 19 views 20 slides Aug 28, 2025
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

Seminar


Slide Content

Definition:PHP mperter processor developed by resume err in 1994,

PHP 5 open source Interpreted and obec vented scripting language executed at server sie
Hs used to develop web appications. PHP s dividsiy used to language on server Is re to download is deep
enough to run the largest socal network

+ Benefits of PHP

1, Freeofcost

3. Platform independent

A. Spon où major web servers
5. Supports al major databases
5. Foster developments
Php features

1. Esseotlesming

2 opensource

3. Growth platform company
A. Large community resources
5. Performance

6 Feli

7. Security features

+ Basic BHP syntax

PHP scripts executed on the server and th plane HTML ests sent back othe browser. APHP seit can
be placed anywhere inthe document

[AHP seit stars with greater than question mark PHP and as wit <?php and nds with?

oh

UP code.

‘The default Ale extension for PHP “php” PHP statements and wth the semicon. be happy normal
continue text and some PHP sci codes

Below an example ofan simple PHP le with a PHP script hat uses. ul in PHP functon"echo” 2 output the
teat” welcome o PHP Work" One a web page

Variable and operators

Variable

‘These are containers used four storing information

‘arabes amemory location on which we store a value during the execution ofa program.

Does stores the value temporary itis destroyed when tove the execution ofthe program,
Note: variable in PHP already noted wth the leading dolar sen (5)

‘Variables can but do nt need tobe decard before assgnment.

Variable names(identifier)

‘And identiyis a name that dents an entity, Another words we can soy hat ents the name whichis
vento variable. variable can havea short name ie and yr more descriptive name age total volume.

Rules for declaring a variable:
1 Avid about started with $ sen

2. Variable name must start withthe etter Or character:

3. Avarable name an nt tarte number

Php variables scope

Local varibe declared inside a funcion has acl cope
Global: variable declared outside of function has» global scope

State vener functions completes/oxecuted allo variables are deleted

Data types:

Php constants

Constance in php ar ut ike varisle but in case of constant you can nt redefine once you define a constant
in PHP eit

Ai constant name in PHP must str wth the te or under sore (ut no dolar sin before the name of
‘the constant) constants are automaticly lob across the ene PAP seit.

+ Operators are special symbols that perform operations on
variables and values.

+ **Types of Operators in Python:**

1. Armee Operators
2. Relational (Comparison) Operators

2. Logis! operators

4. Assienmen Operators

6. ent Operators

7. Membership Operators

4: Arithmetic Operators: Used for mathematical operations

+ osado

+S subaction|

Mutation

+ soon
+ UT Foor Dison

+ x Modus

xponentaton

Relational Operators**Used to compare two values.

+ >=: Gresterthanor equal to

+ <= stessthan or equal

Logical Operators* Used to combine conditional statements.
+ “and: Returns Tue both statements are true

+ “or :Retums True one of te statements true

+ “not s Returns Tue fte statements false

‘Assignment Operators*** Used to assign values to variables.

+ operators

Assign

FS rAd and sion

+ te catty and assign

+ Je Ode and sien

+ IF Floor aide and ass

+ x= Modus and asian

Exponent and assign

Bitwise Operators**

Used for bit-level operations.

cano
+ rior
por
+ amor

> eectet nit

ET

Identity Operators**** Used to compare memory location.
opere

+ ls Retums True both variables ae te same object

+ snot: Returns Tre if both variables are notte same objet

+ **Table Format:*

Operator Type | Operators I

[arithmetic 1+,

[Relational | 1
[Logical J and,or,not_ 1
[Assignment |

IBituise — 1& LA <62> |
[identity is,ismot 1

I Membership in,notin |

Control structure in Php

Definition

‘The statements in a program are executed top to bottom one by one. if you want to change the
flow of your program then you have to use control statement or control structure.

Php have two types of control statement
Decision control statements

Loop control statement

EE PHP decision making statement are used to perform diferent type of action based on
diferent types of condition

Is there controls instruction can be implemented in a PHP using following types of decision
making statements

o

statement
{else statement
Else if statement

Switch statement

Bl tf statement:
block. Otherwise ki tat block

statement test the condition. Is the definition is true then execute the

Syntax:
A

821030
M)

{

Echo's is greater“

+

output

fate

else inthis statement there are two blocks. is sf lok second i a block the

given condition is true then execute that if block ifthe given condition became false then
execute the ass block

Syntax
Ab=89
i
1

Elset
4

[Bb else if : tips statement is used to perform some action based on more than one
condition

Evite case statement this which statement in PHP is a controlled structure that allow for
execution of different code blocks based on the value of single variable or expression

Loops in PHP :
Loops in PHP are used to execute the same block of code several number of times as specified
Types of loop :

For loop EEJPHP for loops is used to execute block of code, specified number of times.

For each loop [E the four each look only work on Array used to loop through each key pair in
an array.

While loop EE php by loop is used to executable block of code is and as long as the expression
given evaluates or two.

Do while loop PHP2 by lupus to execute a block of code atleast once

Array in PHP
{An array is special variable that can hold multiple values in a single variable.
Types of Arrays

Indexed Array > Uses numeric index (0,12...

fruits = array('Apple”, “Banana”, “Mango”);

Echo Sfritl1]// Banana

Associative Array > Uses named keys

Sage

ray"Ram"=>25, “Shyam"=>30, "Geeta"=>28);

Echo Sagel"Shyam’I; // 30

‘Multidimensional Array > Contains arrays inside arrays

Smarks = arrayl

“Math?” => array("Ram"=>90, "Shyam"=>85),

Science” => array("Ram"=>88, “Shyam’=>92)
2

Echo Smarks{"Science"][’Shyam”|; // 92

Common Array Functions
CountíSarr) > Number of elements

Array_merge($a, $b) > Combine arrays

Array_pushlSarr val") > Add element

Array_pop($arr) > Remove last element
Sort($arr / rsort($arr)-> Sort ascending/descending
Array_keys(Sarr) array_values(Sarr) > Get keys/values

9 What isa String in PHP?

A String is a sequence of characters (letters, numbers, symbol).

Examples: "Hello", “123°, "PHP is great!”

In PHP, a string can be written inside single quotes.

© Ways to Declare a String

php

HA. Single quoted sting

1/2, Double quoted string

Sereet = “Hello $name"; // evaluates variable

1/3. Heredoe syntax

This is along string
That spans multiple lines.

£00;

1/4. Nowdoc syntax variables won't be evaluated)
Stext2 = <<< £00!

This is also a string

But variables like $name will not expand

£00;

or double quotes (").

Echo Sereet;

© Single Quotes vs Double Quotes.

Single quotes (”) > keeps text as tis. Variables and escape sequences don't work (except \\
andy)

Double quotes (") > expands variables and understands escape sequences like \n, \t.

one

Worl

Echo ‘Hello $a‘; // Output: Hello Sa
Echo “Hello $a"; // Output: Hello World

© String Functions in PHP

PHP provides many built-in string functions.
Function — Description

Strlen($str) Returns length ofa string
Str_word_countíSstr) Counts number of words.
Strrev(ßstr) Reverses the string
Strpos(Sstr, "word") Finds position of a word

Str_replaco(“old’, “new”, $str) Replaces text in a string

Strtolower($str) _ Converts all characters to lowercase
Strtoupper($str) Converts all characters to uppercase
Vcfirs(Sstr) Capitalizes the first letter

ewords($str) Capitalizes frst letter of each word
Substr(Sstr start, length) Extract part of a string

Trim($str) __ Removes spaces from start and end

Function;

PHP function isa piece of code that can be reused in many times. It can take input as argument
list and return value.

‘Advantages:
1. Reusablty funtion allow you to define a block of code once and reuse it multiple times
throughout your script

2. Easier maintenance: when a function needs to be updated or fixed you only need to
modif the function definition rather than changing code in multiple places.

3. Incapulation function help in capsulate functionality meaning that the internal workings
of function are hidden from the rest of the code,

4, Parameterization: functions can accept parameters allowing you to pass different value
of the function and perform operation based on those values.

5. Inhanced debbuggl: functions in debugging by isolating code into distinct section.

6. que : how function are created in PHP and how you will pass parameters to it Ans: but
‘Ym meters toa function can be passed by two ways

7. Call by value(BHP allows you to call function by value. Actual value is not modified itis
‘modified inside the function. It means changing the value ina function does not affect
the original value when the function i called.

8. Call by reference(PHP allow you to cal function by reference. Actual value is modified if
¡ts modified inside the function. It means changing the value in a function affect the
original value when the function is calle.

What is a Form in PHP?

À form in PHP is an HTML form that allows users to enter data, which can then be sent to a PHP
script for processing (lke saving in a database, checking login credentials, etc).

Forms in PHP usually use the <form> tag in HTML and communicate using either:
+ GET method > Data is sent in the URL.

+ POST method > Data is sent in the request body (more secure),

‘process.php” method="post”>

<label for="name”>Name:</label>

<input type="text” name="name” id="name"><br><br>

<label for="email">Em:

I:</label>

<input type="email” name="email” id="email”><br><br>

<input type="submit” value

Submit”>

</form>

"post” > Sends data securely.

© Types of Form Controls in PHP
Since PHP processes HTML forms, all HTML controls can be used!
1. Text Box (input type="text”) ~ Enter text.

2. Password (input typ

assword”) Enter hidden text.

3. Radio Buttons (input type="radio") ~ Select one option.

4. Checkbox (input type="checkbox") - Select multiple options
5. Drop-down list (<select>) - Choose from a it

6. Text Area (<text area>) - Mult-ine input

7. File Upload (input type="file")~ Upload fils.

8. Button / Submit / Reset ~ Perform action

‘process.php” > The PHP file that will handle the form

PHP session_start) Function

The function named session-start (lis used to star section in PHP. Let’s learn how to start a
session using PHP.

<php
Session start)

Advantages of session

1. **Secure Storage of Data"
Session datas sooredon the sere, nt onthe cent browser

Reduces rik of tampering compared to cookies (which are stored onthe cent de.
E 2. **Abiityto Store Large Data**

Unlike cookies, sessions can store large amounts of data without affecting browser
performance.

CES

Sessions allow data to be shared across multiple pages during a users visit (e.g, maintaining
login state)

ent Across Pages*

E 4. “Better Performance for Sensitive Data**
Sat it db You're asking **why we create cookies in PHP""

In PHP, we create cookies because:

uu © Purpose of Cookies in PHP:

1. **Store data on client side**

* Unlike sessions (which store data on the server), cookies are saved in the user's browser.

* Example: remembering a username for login.

2. *"Maintain login state"

+ A cookie can hold a session ID or authentication token so the user stays logged in

3, **Save preferencest*

‘+ Example: store theme ('dark / light) or language preference.

4, "Track user activity"

+ Cookies can log pages visited, last login time, et.

### © How to Create Cookie in PHP:

We use the **"setcookie()"** function.
sg
php

IT setcookielname, value, expire, path, domain, secure, httponiy);

// Example: creating a cookie
Setcookie(“username”, “Sheetal, time() + (86400 * 7),"/");

1] Cookie “username” wil ast for 7 days

cho “Cookie has been setl”;

tit: © How to Access Cookie:

“php
php
Afisset($_COOKIET" username "]) {

Echo “Welcome back". $_COOKIE "username" ];
else {

Echo “Hello, Guest!”;

EA summary

We create cookies in PHP to store **small client-side data** like login info, preferences, or

session IDs so the website can “remember” the user between visits

Do you want me to also show you an example **difference between Cookie and Session in
PHP** with code?

Sensitive information lke user credentials) is safer in a session because it stays on the server.
KA 5. **Automatic Expiry”

Sessions have a time limit (can be configured), reducing security risks if the user leaves without
logging out.

6. **Easy to Implement**

Most server-side languages (PHP, Python, Java, etc.) provide built-in session handling, making
them simpl

**COOKIE**

A cookie in PHP isa data stored on user's computer. Cookie’s data is sent from a website and
stored on local computer or user's computer by web browser of that local or user's computer,
Cookie’s data is used just for identification purpose for each and every users and to provide litle
better user experience when browsing the same website next time.

In PHP, we create cookies because:
Purpose of Cookies in PHP:
1. **Store data on client-side**

* Unlike sessions (which store data on the server), cookies are saved in the user's browser

* Example: remembering a username for login.

+ A cookie can hold a session ID or authentication token so the user stays logged in
3. **Save preferencest*

‘+ Example: store theme (‘dark / light) or language preference,

A. "Track user activity"

+ Cookies can log pages visited, last login time, et.

How to Create Cook

in PHP

We use the **"setcookie()"** function.

“php
Zoo

11 setcookielname, value, expire, path, domain, secure, htponly)
Il Example: creating a cookie

Setcookie("username”, “Sheetal”, time() + (86400 * 7),

{I Cookie “username” will last for 7 days
Echo “Cookie has been setl”;
Hitit © How to Access Cookie:
ps
<php
Ifisser($_COOKIEI"username”)) {
Echo “Welcome back". $_COOKIE/"username"];
else {

Echo “Hello, Guestl”;