The If and Switch branching statements in PHP
The conditional statement is used to perform different actions based on different conditions.
I. What is the conditional statement in PHP?
A conditional statement is a statement used to change the flow of a program based on a certain condition.
Usually when you write code, you want to perform different actions for different conditions, then you can use conditional statements to do this.
In PHP, we have the following conditional statements:
- if statement - executes some code if a condition is true.
- if... else statement - executes the code if a condition is true and another code if that condition is false.
- if... elseif... else statement - executes different code for more than 2 conditions.
- switch statement - selects one of many code blocks to be executed.
II. IF Statement
The if statement will execute a code if a condition is true.
1. Syntax
if (condition) {
// A piece of code is executed if the condition is true
}
2. Example
We have an example as below: check if number a is even or odd.
$x = 12;
$y = $x % 2; // $y is the remainder. If the remainder is 0, then $x is an even number.
if ($y == 0){
echo 'Number '.$x.' is an even number!';
}
III. IF ... ELSE Statement
The if... else statement executes a piece of code if a condition is true or another code if the condition is false.
1. Syntax
if (condition) {
// The statement is executed if the condition is true.
}else {
// The statement is executed if the condition is false.
}
2. Example
We have an example as below: check if number a is even or odd.
$x = 13;
$y = $x % 2; // $y is the remainder. If the remainder equals 0, then $x is an even number. Otherwise, it is odd.
if ($y == 0){
echo 'Number '.$x.' is an even number!';
}else{
echo 'Number '.$x.' is an odd number!';
{
IV. IF ... ELSE IF... ELSE Statement
The if... elseif... else statement will execute different pieces of code for more than 2 conditions.
1. Syntax
if (condition) {
// The statement is executed if the condition is true.
}else if(condition){
// The statement is executed if the condition is true.
}else{
// The statement is executed if both conditions above are false.
}
2. Example
We have an example as follows: check what color the following color code is.
$color = 'green';
if ($color=='red') {
echo 'This is red color';
}else if($color=='blue'){
echo 'This is blue color';
}else{
echo 'This is green color';
}
V. SWITCH Statement
The switch statement is as meaningful as the if... elseif... else statement but is more concise. This statement is used to execute one of the conditions.
1. Syntax
Note: the break statement is used to prevent code from running through the next conditions.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n does not match any cases above;
}
2. Example
We have an example as follows: check what color the following color code is.
$color = 'red';
switch ($color) {
case 'blue':
echo 'This is blue color';
break;
case 'yellow':
echo 'This is yellow color';
break;
case 'black':
echo 'This is black color';
break;
default:
echo 'This is red color';
}
Submit feedback
Your email address will not be made public. Fields marked are required *
Search
Trend
-
What is Black Myth: Wukong? Detailed Guide on System Requirements and Gameplay
08-21-2024 . 870 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 745 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 726 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 642 view
-
Lost All Money by Trusting in "Elon Musk Deepfake": A Cautionary Tale of Deepfake
08-21-2024 . 609 view
0 feedback