Member-only story
Control Structures in PHP: Using If, Else, and Switch
Control structures are the foundation of logical decision making in PHP programming. They allow the flow of the program to be altered based on certain conditions. Among the control structures in PHP, ‘if’, ‘else’, and ‘switch’ are most commonly used. In this article, we will delve into these control structures and their application in PHP.
If Statement
The ‘if’ statement is used to execute some code only if a specified condition is true.
<?php
$x = 10;
if ($x > 5) {
echo "The number is greater than 5."; // This will be output
}
?>Else Statement
The ‘else’ statement is used to execute some code if the same condition is false.
<?php
$x = 10;
if ($x < 5) {
echo "The number is less than 5.";
} else {
echo "The number is not less than 5."; // This will be output
}
?>Elseif Statement
The ‘elseif’ statement is used to add more conditions in the ‘if’ statement.
<?php
$x = 10;
if ($x < 5) {
echo "The number is less than 5.";
} elseif ($x < 15) {
echo "The number is less than 15."; // This will be output
} else {
echo "The number is not less than 15.";
}
?>





