Conditional Statement
What is Conditional Statements in PHP
Conditional statements are the set of commands used to performed different actions based on different conditions.
In PHP we have the following
- if
- if else
- else if
- switch
if statement in PHP
if structure is used for conditional execution of code segment.
Syntax-
1 2 3 4 |
if(expr) { statements } |
Example-
1 2 3 4 5 6 |
<?php if($c>$d) { echo "c is greater than d"; } ?> |
In the above example ,only if the condition “$c>$d” is true, the message “”c is greater than d” is displayed.
Else Statement in PHP
The conditional statement “else” is used as extension of “if” statement. If the condition fails then it executes another statements under the “else” condition.
Syntax-
1 2 3 4 5 6 7 8 |
if(expr) { statements }//true else { statements }//false |
Based on the result of expressions, the statements are executed.
Example –
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $c=10; $d=20; if($c>$d) { echo "c is greater than d"; } else { echo "d is greater than c"; } ?> |
Result – d is greater than c
In the above example in the if condition “$c>$d” is true then the message “c is greater than d” is displayed , else the message “d is greater than c” is displayed.
Else if Condition
else if condition is used as extension of “if” structure if the condition fails then it executes another “If” condition to execute the code segment under the “else if” statement.
Syntax –
1 2 3 4 5 6 7 8 9 10 11 12 |
if(expr 1) { statements }// true elseif(expr 2)// false { statements }// true else { statements }// false |
Based on the failure “expr 1” condition, “expr 2” is checked and then the statements are executed.
Example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $c=10; $d=10; if($c>$c) { echo "c is greater than d"; } elseif($c==$d) { echo "c is equal to d"; } else { echo "d is smaller than c"; } ?> |
Output- c is equal to d
In the above example the if the condition “$c>$d” is true then the message “c is greater than d” is displayed, else the condition in the else if that is “$c==$d” is evaluated if it is true the message “c is equal to d” is displayed otherwise d is smaller than c is displayed.
Switch Statement
The switch case statement is used to compare a variable or expression values based on which a set of code executed.
Syntax-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
switch(variable or expression) { case (value 0) { statements } case (value 1) { statements } .............. case (value n) { statements } default { statements } } |
Example-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $c=3; switch($c) { case 0: echo "value of $c=0<br>"; break; case 1: echo "value of $c=1<br>"; break; case 2: echo "value of $c=2<br>"; break; default: echo "value of $c=Default value <br>"; break; } ?> |
Output- value of 3 = Default value
In the above example based on the value of $c the messages are displayed of that particular case which matches. The default case accepts anything not matched by other cases, so the value “3” displays the default message.