PHP conditional constructs are used to make decisions. Programmers check the different conditions and run blocks of code accordingly. It is one of the key features of any programming language.
The following are the conditional constructs in PHP:
If
condition:
PHP
if
condition is used where one condition is true. It does not show anything if the condition is false.
Syntax:
1 2 3 4 5 | <?php if(condition) { //Code Block } ?> |
Let’s take an example:
1 2 3 4 5 6 | <?php $num = 10; if($num == 10) { echo "The number is equal to 10"; } ?> |
In the above example,
$num
is matching the value 10 so the output will be show “The number is equal to 10”. If you will change the value of
$num
then it will not show any output because the condition will not match.
If..else
condition
It executes if the condition is true or false.
Syntax:
1 2 3 4 5 6 7 | <?php if(condition) { //Code Block for conditions match } else { //Code Block for conditions do not match } ?> |
Example:
1 2 3 4 5 6 7 8 | <?php $num = 10; if($num == 10) { echo "The number is equal to 10";//It will show as output. } else { echo "The number is not equal to 10"; } ?> |
1 2 3 4 5 6 7 8 | <?php $num = 10; if($num == 9) { echo "The number is equal to 10"; } else { echo "The number is not equal to 10";//It will show as output. } ?> |
In the above example, the assigned value of the variable($num) is not equal to “9” so else part will be run.
If..elseif..else
condition
It is used if you have multiple conditions and run different blocks of code based on those conditions.
Syntax:
1 2 3 4 5 6 7 8 9 | <?php if (condition1) { //This block of code will run if this condition is true } elseif (condition2) { //This block of code will run if condition1 is false and this condition is true } else { //This block of code will run if condition1 and condition2 are false; } ?> |
Example:
1 2 3 4 5 6 7 8 9 10 11 | <?php $a = 20; $b = 15; if ($a > $b) { echo $a . " is greater than " . $b; } elseif ($a == $b) { echo $a . " is equal to " . $b; } else { echo $a . " is less than " . $b; } ?> |
In the above example, the output will be show “20 is greater than 10”.
If the value of
$a
and
$b
will same then below mentioned block of code will run:
1 | echo $a . " is equal to " . $b; |
If the value of $a is less than the value of $b then below mentioned block of code will run:
1 | echo $a . " is less than " . $b; |
Switch/Case Statement
The PHP
switch
statement is used to execute multiple conditions of one statement. It works as a PHP
if..elseif..else
statement.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php switch(expression) { case value: //Block of code break; case value2: //Block of code2 break; default: //default block of code break; } ?> |
Let’s go into more detail on the syntax of the switch statement:
- Place a variable or expression within parentheses into the switch keyword and start({) / end(}) curly braces.
- Inside the curly braces, write multiple cases and place value to compare.
- Write a
default
construct if the value of the case will not math then the default construct will run. - Write
break
statement in each case and default statement.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php $a = 10; $b = 5; $operator = '+'; switch($operator) { case '+': echo $a + $b; break; case '-': echo $a - $b; break; case '*': echo $a * $b; break; case '/': echo $a / $b; break; case '%': echo $a % $b; break; default: echo "Enter correct operator."; break; } ?> |
In the above example, The
$operator's
value is “+” so the case ‘+’ will run if you change the value of
$operator
then the blocks of code will run according to that value.
Please write in the comment if you have any queries or suggestions.