the statements in a program execute from the first to the last statement order of flow in a program set of statements only when a... if statementthe specified condition is true condition
Trang 1Conditional Statements in PHP
Session 7
Trang 2 Operators are pre-defined symbols that enable us perform specific actions
Operators enable us to control the
program flow
The types of operators are:
Arithmetic operators
Logical Operators
Relational Operators
Bitwise Operators
Assignment Operators
Review - I
Trang 3Review - II
Arithmetic operators are used to perform mathematical operations
Relational operators compare two operands and determine the relationship between
operands
Logical operators handle multiple
conditions and combines two or more test expression in a condition
Bitwise operators operate on the bits of
Trang 4Objectives
Trang 5the statements in a program execute
from the first to the last statement
order of flow in a program
set of statements only when a
Trang 6if statement
the specified condition is true
condition is false
if(condition)
// Code executes if the condition is true
Trang 7if statement - Example
$a=50;
$b=100;
if($a < $b)
echo "a is less than b";
$a < $b
PHP displays, a is less than b
Trang 8if …else statement
specified condition is false
if(condition)
{
//Code executes if the condition is true }
else
{
//Code executes if the condition is false
Trang 9if …else statement - Example
if($a < $b)
{
echo “A is less than B”;
}
else($a > $b)
{
echo “B is less than A”;
}
Trang 10Nested if statement
else statement
if(condition)
{
//code to be executed }
else
{
//code to be executed if(condition)
{
//code to be executed
Trang 11Nested if statement - Example
if($a < 500)
{
echo “A is les than 500”;
}
else
{
if ($a >= 500 and $a <= 700)
{
echo “A is between 500 and 700”;
else
{
Trang 12switch statement - I
multiple values
the value it matches
value of the case statement
matches the value of the switch
variable
Trang 13Syntax of switch statement
switch(variable)
{
case value1:
//Code executes if condition equals value1
break;
case value2:
//Code executes if condition equals value2
break;
default:
Moves the control to the statements
Trang 14Example for a switch Statement
switch($var)
{
case Minor:
echo “Not eligible to vote”;
break;
case Major:
echo “Eligible to vote”;
break;
default:
echo “Enter correct value”;
break;
};
Trang 15Ternary Operator
one line statements
Term1 ? Term2 : Term3;
Term1 - Condition is evaluated
Term2 - Executed if condition is True
Trang 16Example of Ternary Statement
$x = 100;
$y = 50;
$disp =
($x > $y) ? “X is greater than Y” : “Y is greater than X”;
Condition checked
Trang 17Summary - I
Conditional statements execute a set of
statements only when a specified
condition is satisfied
if statement executes a block of code
only when the specified condition is true
In a Nested if statement, we specify if
statement within if statement or else
statement
switch statement checks single variable
against multiple values
switch statement executes a block of code
Trang 18Summary - II
the statements following the switch statement
of the case statements matches the
value of the switch variable
conditions into one line statements