One of the key features of PHP is its ability to make decisions based on certain conditions using control structures such as “if,” “if…else,” “if…elseif…else,” and “switch.”
We will take a closer look at each of these control structures and provide code examples to help you understand how to use them.
if Statement
The “if” condition is used to perform a specific action if a certain condition is true. The basic structure of an “if” statement in PHP is as follows:
if (condition) {
// Code to be executed if condition is true
}
For example, the following code will check if a variable called $x is equal to 5, and if it is, it will print x is equal to 5..
$x = 5;
if ($x == 5) {
echo "x is equal to 5";
}
if…else Condition
The “if…else” condition is used when you want to perform a specific action if a certain condition is true and another action if the condition is false.
The basic structure of an “if…else” statement in PHP is as follows:
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
For example, the following code will check if a variable called $x is greater than 5, and if it is, it will print x is greater than 5.. If it is not, it will print x is not greater than 5..
$x = 5;
if ($x > 5) {
echo "x is greater than 5";
} else {
echo "x is not greater than 5";
}
if…elseif…else Condition
The “if…elseif…else” condition is used when you want to check multiple conditions and perform a specific action based on which condition is true.
The basic structure of an “if…elseif…else” statement in PHP is as follows:
if (condition1) {
// Code to be executed if condition1 is true
} elseif (condition2) {
// Code to be executed if condition2 is true
} else {
// Code to be executed if none of the conditions are true
}
For example, the following code will check if a variable called $x is greater than 5, and if it is, it will print x is greater than 5..
If it is not, it will then check if $x is equal to 5, and if it is, it will print x is equal to 5..
If neither of those conditions are true, it will print x is less than 5..
$x = 5;
if ($x > 5) {
echo "x is greater than 5";
} elseif ($x == 5) {
echo "x is equal to 5";
} else {
echo "x is less than 5";
}
Switch Statement
A switch statement is used to perform different actions based on different conditions. The basic structure of a switch statement in PHP is as follows:
switch (expression) {
case value1:
// Code to be executed if expression = value1
break;
case value2:
// Code to be executed if expression = value2
break;
case value3:
// Code to be executed if expression = value3
break;
default:
// Code to be executed if expression does not match any of the cases
}
For example, the following code will check the value of a variable called $x and perform a specific action based on its value.
$x = 2;
switch ($x) {
case 1:
echo "x is equal to 1";
break;
case 2:
echo "x is equal to 2";
break;
case 3:
echo "x is equal to 3";
break;
default:
echo "x is not equal to 1, 2, or 3";
}
In this example, if $x is equal to 2, the code will print x is equal to 2.. If it is not equal to any of the specified cases, it will execute the code under the default statement.
In conclusion, the if, if…else, if…elseif…else, and switch statements are control structures in PHP that allow you to make decisions and perform actions based on certain conditions.
Understanding how to use these control structures can help you write more efficient and effective code.