2. Conditional Statement Conditional statements are the set of commands used to perform different actions based on different conditions. In PHP we have the following conditional statements: If If else Else if switch WWW.USTUDY.IN
3. If statement If structure is used for conditional execution of code segment Syntax: if (expr) { Statements } WWW.USTUDY.IN
4. Example <?php if ($c > $d) { echo "c is bigger than d"; } ?> In the above example, only if the condition "$c>$d" is true, the message "c is bigger than d" is displayed WWW.USTUDY.IN
5. Else statement 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: if (expr) { Statements }//true else { Statements }//false Based on the result of expressions, the statements are executed. WWW.USTUDY.IN
6. Example <?php $c = 10; $d = 20; if ($c > $d) { echo "C is bigger than d"; } Else { echo "D is bigger than c"; } ?> WWW.USTUDY.IN
7. Result Result: D is bigger than C In the above example in the if the condition "$c>$d" is true then the message "C is bigger than D" is displayed, else the message "D is bigger than C" is displayed. WWW.USTUDY.IN
8. Else if statement else if statement 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 WWW.USTUDY.IN
9. Syntax if (expr 1) { Statements }//true elseif (expr2)//false { Statements }//true Else { Statements }//false Based on the failure "expr1" condition, "expr2" is checked and then the statements are executed. WWW.USTUDY.IN
10. Example <?php $c = 10; $d = 10; if ($c > $d) { echo "c is bigger than d"; } elseif ($c==$d) { echo "c is equal to d"; } else { echo "d is smaller than c"; } ?> WWW.USTUDY.IN
11. Result c is equal to d In the above example the if the condition "$c>$d" is true then the message "c is bigger 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 WWW.USTUDY.IN
12. Switch statement The Switch case statement is used to compare a variable or expression to different values based on which a set of code is executed. WWW.USTUDY.IN
13. Syntax Switch (Variable or expression) { Case (value 0): { Statements } Case (value 1): { Statements } ………… Case (value n): {statements } default: { Statements } } WWW.USTUDY.IN
14. Example <?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; } ?> WWW.USTUDY.IN
15. Result 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. WWW.USTUDY.IN