Conditional statement

1,501 views

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,501
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Conditional statement

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

×