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
‘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
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),
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