SlideShare a Scribd company logo
PHP: Hypertext Preprocessor
What is PHP:
"PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
PHP is an amazing and popular language!
It is powerful enough to be at the core of the biggest blogging system on
the web (WordPress)!
It is deep enough to run large social networks!
It is also easy enough to be a beginner's first server side language!
What is a PHP File:
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the
browser as plain HTML
PHP files have extension ".php"
What Can PHP Do:
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
• Why PHP:
• PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
• PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
• PHP supports a wide range of databases
• PHP is free. Download it from the official PHP resource: www.php.net
• PHP is easy to learn and runs efficiently on the server side
EXAMPLE:
When such an HTML documents is requested by user, a PHP aware Web server can
recognize and execute the PHP code blocks and interpolate the resulting output into
the HTML documents before returning it to the requesting user. The result: a Web
page or application that almost seems alive, responding intelligently to user actions
by virtue of PHP program logic embedded within it. Fig 1-1 illustrates the process,
showing the four elements of the LAMP framework, described later in this section
Here’s what’s happening in fig1-1:Joe pops open his Web browser at home and
types in the URL to a Web site. After looking up domain, Joe’s browser(the client)
sends an HTTP request to the corresponding server IP address.
The Web server handling HTTP request for the domain receives the request and
notes that URL end with a .php suffix. Because the server is programmed to
automatically redirect all such requests to the PHP layer, it simply invokes the PHP
interpreter and passes it the contents of the named file.
The PHP interpreter parses the file, executing the code in the special PHP tags.
Within these tags, you can perform calculations, process user input, interact with a
database, read and writes files… the list goes on! Once the script interpreter has
completed executing the PHP instructions, it returns the result to the browser, clean
up after itself, and goes back into hibernation.
PHP2An introduction to Gnome.pptx.j.pptx
The results retuned by the interpreter are transmitted to Joe’s browser by
the Web server.
From the preceding explanation, it should be clear that to get started
building PHP applications, your development environment must
contains at least three components:
1. A base operating system(OS) and server environment(usually
Linux).
2. A Web server(usually Apache on Linux or IIS on Windows) to
intercept HTTP requests and either serve them directly or pass
them on to the PHP interpreter for execution.
3. A PHP interpreter to parse and execute PHP code, and return the
results to the Web server.
Unique Features of PHP:
Performance Scripts written in PHP execute faster than those written in other
scripting languages.
1. Portability PHP
2. Ease to Use
3. Open Source
4. Community Support
5. Third-Party Application Support
First PHP Basic Script:
1. <?php
echo “My first PHP Script”;
?>
2. < html>
<title>Getting Started With PHP</title>
<body>
<?php
echo”Hello”;
?>
</body>
</html>
PHP Variables
A variable is a container holding a certain value.
Syntax
Variables in PHP beginning with $ followed by a letter or
an underscore, then any combination of letters,
numbers, and the underscore character.
Here are the rules we would follow to name variables.
Variable names begin with a dollar sign ( $ )
The first character after the dollar sign must be a letter
or an underscore
The remaining characters in the name may be letters,
numbers, or underscores without a fixed limit
Define PHP variables
Variable Description
$myvar Correct
$Name Correct
$_Age Correct
$___AGE___ Correct
$Name91 Correct;
$1NameIn correct; starts with a number
$Name's Incorrect; no symbols other than "_" are allowed
Creating (Declaring) PHP Variables
In PHP, a variable starts with the $ sign, followed by the name
of the variable:
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Output:
Hello world!
5
10.5
Display Variables:
<html>
<body>
<?php
$txt1 = "Learn PHP";
$txt2 = "do ur programs";
$x = 5;
$y = 3;
echo "<h2>" . $txt1 . "</h2>";
echo "If u want to score marks in lab " . $txt2 . "<br>";
echo $x + $y;
?>
</body>
</html>
PHP Variables
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
Rules for PHP variables:
1. A variable starts with the $ sign, followed by the name of
the variable
2. A variable name must start with a letter or the underscore
character
3. A variable name cannot start with a number
4. A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
5. Variable names are case-sensitive ($age and $AGE are two
different variables)
6. Remember that PHP variable names are case-sensitive!
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the
variable can be referenced/used.
PHP has three different variable scopes:
 local
 global
 Static
PHP2An introduction to Gnome.pptx.j.pptx
1. Local variables are those variables that are declared inside the function of
a Php program and have their scope inside that function only.
<html>
<body>
<?php
//php function
function myLocal() {
// local variable ‘name’ having the local scope
$name = ‘Katherine';
echo "<p>Hello the value of local variable inside the function is : $name
</p>";
}
//calling the function
myLocal();
// printing the value of local variable outside the function, gives an error
echo "<p>Value of local variable outside the function is : $name </p>";
?>
</body>
</html>
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function:
1.Example
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y`;
$y = $x + $y;
}
myTest(); // run function
Echo $y; // output the new value for variable $y
?>
</body>
</html>
2. <html>
<body>
<?php
// global variable
$name = ‘Jeni';
function myLocal() {
$name = ‘Lidia'; // local variable having the local scope
echo "<p>Hello the value of local variable inside the function is : $name
</p>";
}
//calling the function
myLocal();
// printing the value of variable outside the function, will consider the
global function
echo "<p>Value of variable outside the function is : $name </p>";
?>
</body>
</html>
PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need
it for a further job.
To do this, use the static keyword when you first declare the variable:
<html>
<body>
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
OUTPUT:
0
1
2
PHP Operators:
Operators are used to perform operations on variables and
values.
PHP divides the operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/Decrement operators
5. Logical operators
6. String operators
7. Array operators
8. Conditional assignment operators
<html>
<body>
<?php
// You can also use comments to leave out parts of a code line
$a = 5+3;
$b = 5-3;
$c = 5*3;
$d = 5/3;
echo "$a";
echo "<br>";
echo "$b";
echo "<br>";
echo "$c";
echo "<br>";
echo "$d";
echo "<br>";
?>
</body>
</html>
OUTPUT:
8
2
15
1.6666666666667
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a
value to a variable.
The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the right.
PHP Assignment Operators
Operator Name Description
= Assignment $a = $ b copies $b's value into $a
=& Reference $a =& $b set $a to reference $b
<?PHP
$x = 4;
$x = $x + 4; // $x now equals 8 print $x;
?>
Assignment Operator Example
<?php
//Add 5 to Count
$Count = 0;
$Count = $Count + 5;
//Add 5 to Count
$Count = 0;
$Count += 5;
//prints 13 print($a = $b = 13);
print("<br>n");
//prints 7 $Count = 2;
print($Count += 5);
print("<br>n");
?>
PHP Comparison Operators
Comparison operators return either true or false, and thus are suitable for use
in conditions.
Comparison operators in PHP has are listed in the following table.
Operator Name Description
== EqualsTrue if $a is equal to $b
=== IdenticalTrue if $a is equal to $b and of the same
!= Not equalTrue if $a is not equal to $b<>Not
equalTrue if $a is not equal to $b
!= = Not identicalTrue if $a is not equal to $b or if they are not
of the same type
< Less thanTrue If $a is less than $b>Greater thanTrue if
$a is greater than $b
<= Less than or equal True if $a is less than or equal to $b
>= Greater than or equal True if $a is greater than or equal to $b
The === (identical) says two variables are only identical if they hold the same
value and if they are the same type, as demonstrated in this code
<?PHP
print 12 == 12;
print 12.0 == 12;
print (0 + 12.0) == 12;
print 12 === 12;
print "12" == 12;
print "12" === 12;
?>
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a
variable's value.
The PHP decrement operators are used to decrement a
variable's value.
PHP Logical Operators
The PHP logical operators are used to combine conditional
statements.
PHP Array Operators
The PHP array operators are used to compare arrays.
PHP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a
value depending on conditions
Setting and Checking Variables:
The isset() function checks whether a variable is set, which
means that it has to be declared and is not NULL. This
function returns true if the variable exists and
 Return Value:TRUE if variable exists and is not NULL, FALSE
otherwise.
 Return Type:Boolean
 PHP Version:4.0+PHP Changelog:PHP 5.4: Non-numeric
offsets of strings now returns FALSE is not NULL, otherwise it
returns false.
<?php
$a = 0;
// True because $a is set
if (isset($a)) {
echo "Variable 'a' is set.<br>";
}
$b = null;
// False because $b is NULL
if (isset($b)) {
echo "Variable 'b' is set.";
}
?>
 Constant
To create a constant, use the define() function.
Syntax
define(name, value, case-insensitive)
Parameters:
 name: Specifies the name of the constant
 value: Specifies the value of the constant
 case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
<html>
<body>
<?php
// case-sensitive constant name
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
?>
</body>
</html>
<html>
<body>
<?php
// case-insensitive constant name
define("GREETING", “WELCOME TO PHP!", true);
echo greeting;
?>
</body>
</html>
Constant Arrays
<html>
<body>
<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
?>
</body>
</html>
Constants are Global
Constants are automatically global and can be used across the entire script.
<html>
<body>
<?php
define("GREETING", "Welcome to PHP!");
function myTest() {
echo GREETING;
}
myTest();
?>
</body>
</html>
PHP Conditional Statements
Very often when you write code, you want to perform
different actions for different conditions. You can use
conditional statements in your code to do this.
In PHP we have the following conditional statements:
1. if statement - executes some code if one condition is
true
2. if...else statement - executes some code if a condition is
true and another code if that condition is false
3. if...elseif...else statement - executes different codes for
more than two conditions
4. switch statement - selects one of many blocks of code to
be executed
PHP - The if Statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
Example:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
PHP - if...else Statement
if...else statement executes some code if a condition is true and another code if
that condition is false.
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
EXAMPLE:
<html>
<body>
<?php
$age =20;
if ($age < "25") {
echo "Eligible!";
} else {
echo "Not eligible!";
}
?>
</body>
</html>
if...elseif...else Statement
The if...elseif...else statement executes different codes for more than two
conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
Example:
<?php
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The PHP switch Statement
Use the switch statement to select one of many blocks of code to be
executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>

More Related Content

PDF
Web Development Course: PHP lecture 1
PPTX
Introduction to php
PDF
Introduction to PHP - Basics of PHP
PDF
PHP in Web development and Applications.pdf
PPT
Intro to PHP for Students and professionals
PDF
1336333055 php tutorial_from_beginner_to_master
PDF
WT_PHP_PART1.pdf
PPTX
Php intro by sami kz
Web Development Course: PHP lecture 1
Introduction to php
Introduction to PHP - Basics of PHP
PHP in Web development and Applications.pdf
Intro to PHP for Students and professionals
1336333055 php tutorial_from_beginner_to_master
WT_PHP_PART1.pdf
Php intro by sami kz

Similar to PHP2An introduction to Gnome.pptx.j.pptx (20)

PPTX
introduction to php and its uses in daily
PDF
Wt unit 4 server side technology-2
PPTX
PPTX
php Chapter 1.pptx
PPT
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
PPT
Php i basic chapter 3
PPT
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
PDF
Programming in PHP Course Material BCA 6th Semester
PPTX
Php Tutorial
PDF
basic concept of php(Gunikhan sonowal)
PPT
PHP - Introduction to PHP - Mazenet Solution
PDF
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PPTX
PHP Basics
ODP
PHP Basic
DOCX
PHP NOTES FOR BEGGINERS
PPTX
Php assignment help
introduction to php and its uses in daily
Wt unit 4 server side technology-2
php Chapter 1.pptx
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Programming in PHP Course Material BCA 6th Semester
Php Tutorial
basic concept of php(Gunikhan sonowal)
PHP - Introduction to PHP - Mazenet Solution
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PHP Basics
PHP Basic
PHP NOTES FOR BEGGINERS
Php assignment help
Ad

Recently uploaded (20)

PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PPT
SCOPE_~1- technology of green house and poyhouse
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
6th International Conference on Artificial Intelligence and Machine Learning ...
PPTX
anatomy of limbus and anterior chamber .pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPT
High Data Link Control Protocol in Data Link Layer
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Soil science - sampling procedures for soil science lab
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
Glazing at Facade, functions, types of glazing
PPT
Ppt for engineering students application on field effect
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
SCOPE_~1- technology of green house and poyhouse
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
Strings in CPP - Strings in C++ are sequences of characters used to store and...
6th International Conference on Artificial Intelligence and Machine Learning ...
anatomy of limbus and anterior chamber .pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
High Data Link Control Protocol in Data Link Layer
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Soil science - sampling procedures for soil science lab
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Simulation of electric circuit laws using tinkercad.pptx
Glazing at Facade, functions, types of glazing
Ppt for engineering students application on field effect
Ad

PHP2An introduction to Gnome.pptx.j.pptx

  • 2. What is PHP: "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use PHP is an amazing and popular language! It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)! It is deep enough to run large social networks! It is also easy enough to be a beginner's first server side language! What is a PHP File: PHP files can contain text, HTML, CSS, JavaScript, and PHP code PHP code is executed on the server, and the result is returned to the browser as plain HTML PHP files have extension ".php"
  • 3. What Can PHP Do: PHP can generate dynamic page content PHP can create, open, read, write, delete, and close files on the server PHP can collect form data PHP can send and receive cookies PHP can add, delete, modify data in your database PHP can be used to control user-access PHP can encrypt data • Why PHP: • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP supports a wide range of databases • PHP is free. Download it from the official PHP resource: www.php.net • PHP is easy to learn and runs efficiently on the server side
  • 4. EXAMPLE: When such an HTML documents is requested by user, a PHP aware Web server can recognize and execute the PHP code blocks and interpolate the resulting output into the HTML documents before returning it to the requesting user. The result: a Web page or application that almost seems alive, responding intelligently to user actions by virtue of PHP program logic embedded within it. Fig 1-1 illustrates the process, showing the four elements of the LAMP framework, described later in this section Here’s what’s happening in fig1-1:Joe pops open his Web browser at home and types in the URL to a Web site. After looking up domain, Joe’s browser(the client) sends an HTTP request to the corresponding server IP address. The Web server handling HTTP request for the domain receives the request and notes that URL end with a .php suffix. Because the server is programmed to automatically redirect all such requests to the PHP layer, it simply invokes the PHP interpreter and passes it the contents of the named file. The PHP interpreter parses the file, executing the code in the special PHP tags. Within these tags, you can perform calculations, process user input, interact with a database, read and writes files… the list goes on! Once the script interpreter has completed executing the PHP instructions, it returns the result to the browser, clean up after itself, and goes back into hibernation.
  • 6. The results retuned by the interpreter are transmitted to Joe’s browser by the Web server. From the preceding explanation, it should be clear that to get started building PHP applications, your development environment must contains at least three components: 1. A base operating system(OS) and server environment(usually Linux). 2. A Web server(usually Apache on Linux or IIS on Windows) to intercept HTTP requests and either serve them directly or pass them on to the PHP interpreter for execution. 3. A PHP interpreter to parse and execute PHP code, and return the results to the Web server.
  • 7. Unique Features of PHP: Performance Scripts written in PHP execute faster than those written in other scripting languages. 1. Portability PHP 2. Ease to Use 3. Open Source 4. Community Support 5. Third-Party Application Support
  • 8. First PHP Basic Script: 1. <?php echo “My first PHP Script”; ?> 2. < html> <title>Getting Started With PHP</title> <body> <?php echo”Hello”; ?> </body> </html>
  • 9. PHP Variables A variable is a container holding a certain value. Syntax Variables in PHP beginning with $ followed by a letter or an underscore, then any combination of letters, numbers, and the underscore character. Here are the rules we would follow to name variables. Variable names begin with a dollar sign ( $ ) The first character after the dollar sign must be a letter or an underscore The remaining characters in the name may be letters, numbers, or underscores without a fixed limit
  • 10. Define PHP variables Variable Description $myvar Correct $Name Correct $_Age Correct $___AGE___ Correct $Name91 Correct; $1NameIn correct; starts with a number $Name's Incorrect; no symbols other than "_" are allowed
  • 11. Creating (Declaring) PHP Variables In PHP, a variable starts with the $ sign, followed by the name of the variable: Example <?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?> Output: Hello world! 5 10.5
  • 12. Display Variables: <html> <body> <?php $txt1 = "Learn PHP"; $txt2 = "do ur programs"; $x = 5; $y = 3; echo "<h2>" . $txt1 . "</h2>"; echo "If u want to score marks in lab " . $txt2 . "<br>"; echo $x + $y; ?> </body> </html>
  • 13. PHP Variables A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for PHP variables: 1. A variable starts with the $ sign, followed by the name of the variable 2. A variable name must start with a letter or the underscore character 3. A variable name cannot start with a number 4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) 5. Variable names are case-sensitive ($age and $AGE are two different variables) 6. Remember that PHP variable names are case-sensitive!
  • 14. PHP Variables Scope In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes:  local  global  Static
  • 16. 1. Local variables are those variables that are declared inside the function of a Php program and have their scope inside that function only. <html> <body> <?php //php function function myLocal() { // local variable ‘name’ having the local scope $name = ‘Katherine'; echo "<p>Hello the value of local variable inside the function is : $name </p>"; } //calling the function myLocal(); // printing the value of local variable outside the function, gives an error echo "<p>Value of local variable outside the function is : $name </p>"; ?> </body> </html>
  • 17. Global and Local Scope A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: 1.Example <html> <body> <?php $x = 5; $y = 10; function myTest() { global $x, $y`; $y = $x + $y; } myTest(); // run function Echo $y; // output the new value for variable $y ?> </body> </html>
  • 18. 2. <html> <body> <?php // global variable $name = ‘Jeni'; function myLocal() { $name = ‘Lidia'; // local variable having the local scope echo "<p>Hello the value of local variable inside the function is : $name </p>"; } //calling the function myLocal(); // printing the value of variable outside the function, will consider the global function echo "<p>Value of variable outside the function is : $name </p>"; ?> </body> </html>
  • 19. PHP The static Keyword Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable: <html> <body> <?php function myTest() { static $x = 0; echo $x; $x++; } myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); ?> </body> </html> OUTPUT: 0 1 2
  • 20. PHP Operators: Operators are used to perform operations on variables and values. PHP divides the operators in the following groups: 1. Arithmetic operators 2. Assignment operators 3. Comparison operators 4. Increment/Decrement operators 5. Logical operators 6. String operators 7. Array operators 8. Conditional assignment operators
  • 21. <html> <body> <?php // You can also use comments to leave out parts of a code line $a = 5+3; $b = 5-3; $c = 5*3; $d = 5/3; echo "$a"; echo "<br>"; echo "$b"; echo "<br>"; echo "$c"; echo "<br>"; echo "$d"; echo "<br>"; ?> </body> </html> OUTPUT: 8 2 15 1.6666666666667
  • 22. PHP Assignment Operators The PHP assignment operators are used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right. PHP Assignment Operators Operator Name Description = Assignment $a = $ b copies $b's value into $a =& Reference $a =& $b set $a to reference $b <?PHP $x = 4; $x = $x + 4; // $x now equals 8 print $x; ?>
  • 23. Assignment Operator Example <?php //Add 5 to Count $Count = 0; $Count = $Count + 5; //Add 5 to Count $Count = 0; $Count += 5; //prints 13 print($a = $b = 13); print("<br>n"); //prints 7 $Count = 2; print($Count += 5); print("<br>n"); ?>
  • 24. PHP Comparison Operators Comparison operators return either true or false, and thus are suitable for use in conditions. Comparison operators in PHP has are listed in the following table. Operator Name Description == EqualsTrue if $a is equal to $b === IdenticalTrue if $a is equal to $b and of the same != Not equalTrue if $a is not equal to $b<>Not equalTrue if $a is not equal to $b != = Not identicalTrue if $a is not equal to $b or if they are not of the same type < Less thanTrue If $a is less than $b>Greater thanTrue if $a is greater than $b <= Less than or equal True if $a is less than or equal to $b >= Greater than or equal True if $a is greater than or equal to $b The === (identical) says two variables are only identical if they hold the same value and if they are the same type, as demonstrated in this code
  • 25. <?PHP print 12 == 12; print 12.0 == 12; print (0 + 12.0) == 12; print 12 === 12; print "12" == 12; print "12" === 12; ?>
  • 26. PHP Increment / Decrement Operators The PHP increment operators are used to increment a variable's value. The PHP decrement operators are used to decrement a variable's value. PHP Logical Operators The PHP logical operators are used to combine conditional statements. PHP Array Operators The PHP array operators are used to compare arrays. PHP Conditional Assignment Operators The PHP conditional assignment operators are used to set a value depending on conditions
  • 27. Setting and Checking Variables: The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and  Return Value:TRUE if variable exists and is not NULL, FALSE otherwise.  Return Type:Boolean  PHP Version:4.0+PHP Changelog:PHP 5.4: Non-numeric offsets of strings now returns FALSE is not NULL, otherwise it returns false.
  • 28. <?php $a = 0; // True because $a is set if (isset($a)) { echo "Variable 'a' is set.<br>"; } $b = null; // False because $b is NULL if (isset($b)) { echo "Variable 'b' is set."; } ?>
  • 29.  Constant To create a constant, use the define() function. Syntax define(name, value, case-insensitive) Parameters:  name: Specifies the name of the constant  value: Specifies the value of the constant  case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false <html> <body> <?php // case-sensitive constant name define("GREETING", "Welcome to W3Schools.com!"); echo GREETING; ?> </body> </html>
  • 30. <html> <body> <?php // case-insensitive constant name define("GREETING", “WELCOME TO PHP!", true); echo greeting; ?> </body> </html>
  • 31. Constant Arrays <html> <body> <?php define("cars", [ "Alfa Romeo", "BMW", "Toyota" ]); echo cars[0]; ?> </body> </html>
  • 32. Constants are Global Constants are automatically global and can be used across the entire script. <html> <body> <?php define("GREETING", "Welcome to PHP!"); function myTest() { echo GREETING; } myTest(); ?> </body> </html>
  • 33. PHP Conditional Statements Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements: 1. if statement - executes some code if one condition is true 2. if...else statement - executes some code if a condition is true and another code if that condition is false 3. if...elseif...else statement - executes different codes for more than two conditions 4. switch statement - selects one of many blocks of code to be executed
  • 34. PHP - The if Statement The if statement executes some code if one condition is true. Syntax if (condition) { code to be executed if condition is true; } Example: <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } ?>
  • 35. PHP - if...else Statement if...else statement executes some code if a condition is true and another code if that condition is false. if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } EXAMPLE: <html> <body> <?php $age =20; if ($age < "25") { echo "Eligible!"; } else { echo "Not eligible!"; } ?> </body> </html>
  • 36. if...elseif...else Statement The if...elseif...else statement executes different codes for more than two conditions. Syntax if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; }
  • 37. Example: <?php $t = date("H"); echo "<p>The hour (of the server) is " . $t; echo ", and will give the following message:</p>"; if ($t < "10") { echo "Have a good morning!"; } elseif ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
  • 38. The PHP switch Statement Use the switch statement to select one of many blocks of code to be executed. Syntax switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
  • 39. html> <body> <?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?> </body> </html>