Tải bản đầy đủ (.ppt) (18 trang)

Slide môn học PHP session 2c conditional statements in PHP

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (251.17 KB, 18 trang )

Conditional Statements in PHP
Session 7


Review - I






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
String Operators

PHP / Session 7 / Slide 2 of 18




Review - 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
an operand
PHP / Session 7 / Slide 3 of 18


Objectives




Use the if statement

Use the Switch statement
Use the Ternary (?) operator

PHP / Session 7 / Slide 4 of 18


Introduction








Flow refers to the order in which
the statements in a program execute
Statements executes in a sequence,
from
the
first
to
the
last
statement
Conditional statements change the
order of flow in a program
Conditional statements execute a
set of statements only when a
specified condition is satisfied

PHP / Session 7 / Slide 5 of 18


if statement






Executes a block of code only when
the specified condition is true
The block of code is ignored if the
condition is false
The syntax for the if statement:
if(condition)
//Code executes

if the condition is true
PHP / Session 7 / Slide 6 of 18


if statement - Example
$a=50;
$b=100;
if($a < $b)
echo "a is less than b";

$a < $b
PHP displays, a is less than b

PHP / Session 7 / Slide 7 of 18


if …else statement




Executes a block of code when the
specified condition is false
Syntax for if …else statement:
if(condition)
{
//Code executes if the condition is true
}

else
{
//Code executes if the condition is false
}

PHP / Session 7 / Slide 8 of 18


if …else statement - Example
if($a < $b)
{
echo “A is less than B”;
}
else($a > $b)

{
echo “B is less than A”;
}
PHP / Session 7 / Slide 9 of 18


Nested if statement




Specify if statement within if or
else statement
Syntax for nested if statement:
if(condition)
{
//code to be executed
}

else
{
//code to be executed
if(condition)
{
//code to be executed
}
}

PHP / Session 7 / Slide 10 of 18



Nested 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
{
echo “A is greater than 700”;
}
}
}

PHP / Session 7 / Slide 11 of 18


switch statement - I






Checks single variable against
multiple values

Executes a block of code based on
the value it matches
Executes the code only when the
value of the case statement
matches the value of the switch
variable
PHP / Session 7 / Slide 12 of 18


Syntax of switch statement
switch(variable)
{
case value1:
//Code executes if condition equals value1

break;

Moves the control to the statements

case value2:
//Code executes if condition equals value2

break;
default:

Used when none of the case statements matches
the value of the switch variable

//Code executes if the variable does not matches
any specified value


}

PHP / Session 7 / Slide 13 of 18


Example 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;
};
PHP / Session 7 / Slide 14 of 18


Ternary Operator





Also known as conditional operator
Simplifies complex conditions into

one line statements
Syntax of ternary operator:
Term1 ? Term2 : Term3;
Term1 - Condition is evaluated
Term2 - Executed if condition is True
Term3 - Executed if condition if False
PHP / Session 7 / Slide 15 of 18


Example of Ternary Statement
$x = 100;
$y = 50;
Condition checked

$disp =
($x > $y) ? “X is greater than Y” : “Y is greater than X”;
Condition True

Condition False

PHP / Session 7 / Slide 16 of 18


Summary - 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
based on the value it matches
PHP / Session 7 / Slide 17 of 18


Summary - II






break statement moves the control to
the statements following the switch
statement
default statement is used when none

of the case statements matches the
value of the switch variable
Ternary operator simplifies complex
conditions into one line statements
PHP / Session 7 / Slide 18 of 18



×