PHP Conditional Statements
PHP Conditional Statements
Statements
Prepared by: Yvonn Kyla V. Magallon
Source: w3schools.com
Learning Objectives
1.To understand the importance of conditional
statement
2.To know the different types of conditional
statements in PHP
3.To know the fundamental logic creation using
conditional statement
Conditional Statements
Conditional Statements are
used to perform different
actions based on different
conditions.
Types of PHP Conditional Statements
1.if statement
2.if…else statement
3.if…elseif…else statement
4.switch statement
The if statement
The if statement
executes some code if
one condition is true.
if(condition){
code to be executed if condition is true;
}
Syntax
if(condition){
code to be executed
if condition is true;
}
if(condition){
code to be executed if condition is true;
}
Example
<?php
$x = 20;
$y = 10;
if($x > $y){
echo “$x is greater
than $y”;
}
?>
if(condition){
code to be executed if condition is true;
}
Output
20 is greater than 10
if(condition){
code to be executed if condition is true;
}
Example
<?php
$x = 10;
$y = 20;
if($x > $y){
echo “$x is greater
than $y”;
}
?>
The if…else statement
The 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;
}
Syntax
if(condition){
code to be executed if
condition is true;
}else{
code to be executed if
condition is false;
}
if(condition){
code to be executed if condition is true;
}
Example
<?php
$x = 10;
$y = 20;
if ($x > $y){
echo “$x is greater than $y”;
}else{
echo “$x is less than $y”;
}
?>
if(condition){
code to be executed if condition is true;
}
Output
10 is less than 20
Must remember:
1.Conditional Statements are used to perform
different actions based on different
conditions.
2. The if statement executes some code if one
condition is true.
3. The if…else statement executes some code if a
condition is true and another code if that
condition is false.
Thank you for
listening!