Advertisement
sayful

PHP conditions

Aug 10th, 2014
2,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2. /*
  3. if (condition)
  4.   {
  5.   code to be executed if condition is true;
  6.   }
  7. */
  8. $a = 9;
  9. $b = 7;
  10.  
  11. if ( $a > $b ){
  12.     echo 'A is bigger than B';
  13. }
  14.  
  15. if ( $a < $b ){
  16.     echo 'A is smaller than B';
  17. }
  18.  
  19. if ( $a == $b ){
  20.     echo 'A is equal to B';
  21. }
  22. echo '<br><hr>';
  23. /*
  24. if (condition)
  25.  {
  26.   code to be executed if condition is true;
  27.  }
  28. else
  29.  {
  30.   code to be executed if condition is false;
  31.  }
  32. */
  33. if ( $a > $b ){
  34.     echo ' A is bigger than B';
  35. }  else {
  36.     echo 'A is smaller than B';
  37. }
  38.  
  39. echo '<br><hr>';
  40. /*
  41. if (condition)
  42.   {
  43.   code to be executed if condition is true;
  44.   }
  45. else if (condition)
  46.   {
  47.   code to be executed if condition is true;
  48.  }
  49. else
  50.   {
  51.   code to be executed if condition is false;
  52.  }
  53. */
  54. if ( $a == $b ){
  55.     echo 'A is equal to B';
  56. }elseif ( $a < $b ) {
  57.     echo 'A is smaller than B';
  58. }  else {
  59.     echo 'A is bigger than B';
  60. }
  61. echo '<br><hr>';
  62. /*
  63. switch (n)
  64. {
  65. case label1:
  66.   code to be executed if n=label1;
  67.   break;
  68. case label2:
  69.   code to be executed if n=label2;
  70.   break;
  71. default:
  72.   code to be executed if n is different from both label1 and label2;
  73. }
  74. */
  75. $favcolor = 'pink';
  76.  
  77. switch ($favcolor) {
  78.     case "red":
  79.         echo 'Your favourate color is red';
  80.         break;
  81.    
  82.     case "green":
  83.         echo 'Your favourate color is green';
  84.         break;
  85.  
  86.     default:
  87.         echo 'None of this is your favourate color';
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement