Control flow statements decides the execution flow of the PHP program based on certain conditions.
This if..else
statements will allow to execute the code based on the specified condition is true or false.
Syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
$age = 30;
if ($age >= 18) {
echo "You are eligible to cast your vote";
} else {
echo "You are not eligible to cast your vote";
}
It implements the complex control flow of the PHP program with the help of nested if..else
statements.
Example:
$price = 2000;
if ($price > 2000) {
echo "You have discount of 25%";
} else if ($price < 2000 && $price > 1000) {
echo "You have discount of 10%";
} else {
echo "No discount eligible";
}
It provides simplest way to handle and switch to desired condition among multiple conditions with same variable.
Syntax:
switch (expression) {
case value1:
// Code to execute if the expression equals to value1
break;
case value2:
// Code to execute if the expression equals to value2
break;
// more cases if necessary
default:
// Code to execute if the no match found for the expression (optional)
break;
}
Example:
$day = 'monday';
switch ($day) {
case 'monday':
echo "Monday is weekday";
break;
case 'tuesday':
echo "Tuesday is weekday";
break;
case 'wednesday':
echo "Wednesday is weekday";
break;
case 'thursday':
echo "Thursday is weekday";
break;
case 'friday':
echo "Friday is weekday";
break;
default:
echo "Saturday and Sunday are weekends";
break;
}