QUIZGUM

Coding Class

Quizgum : switch (control statement)

control statement - switch

The if statement selects either true or false, while the switch selects one of several.
To use a switch, find and select the value of the variable declared as true among the various cases given.
If the variable $a = 3 is declared, the corresponding case is selected and the process for executing the statement is as follows.

  1. <?php
  2. echo "Switch statement when declaring \$a = 2<br />";
  3.  
  4. $a = 2;
  5.  
  6. switch($a) {
  7. case 0:
  8. echo "The variable \ $a is the number 0 ....<b> case 0:</b><br />";
  9. break;
  10.  
  11. case 1:
  12. echo "The variable \$a is the number 1 ...<b> case 1:</b><br />";
  13. break;
  14.  
  15. case 2:
  16. echo "The variable \$a is the number 2 .... <b> case 2:</b><br />";
  17. break;
  18.  
  19. case 3:
  20. echo "The variable \$a is the number 3 ... <b> case 3:</b><br />";
  21. break;
  22.  
  23. case 4:
  24. echo "The variable \$a is the number 4 ... <b> case 4:</b><br />";
  25. break;
  26.  
  27. default:
  28. echo "The variable \$a = {$a} is a number other than 0,1,2,3.<br />";
  29. }
  30. ?>
php image

Assigning 2 as the value of $a and setting $a as the condition of the switch,
So if you specify case 1, case 2, case 3 case 4
The matched switch is 2, so the execution statements in case 2 are executed.
There is a break in each case statement, which executes the above statement when the condition is met and exits the switch statement when a break statement is encountered.
If you don't declare break you get the following result:

  1. <?php
  2. echo "Switch statement when declaring \$a = 2<br />";
  3.  
  4. $a = 2;
  5.  
  6. switch($a) {
  7. case 0:
  8. echo "The variable \ $a is the number 0 ....<b> case 0:</b><br />";
  9.  
  10. case 1:
  11. echo "The variable \$a is the number 1 ...<b> case 1:</b><br />";
  12.  
  13. case 2:
  14. echo "The variable \$a is the number 2 .... <b> case 2:</b><br />";
  15.  
  16. case 3:
  17. echo "The variable \$a is the number 3 ... <b> case 3:</b><br />";
  18.  
  19. case 4:
  20. echo "The variable \$a is the number 4 ... <b> case 4:</b><br />";
  21.  
  22. default:
  23. echo "The variable \$a = {$a} is a number other than 0,1,2,3.<br />";
  24. }
  25. ?>
php image

Since the break statement is not declared, the following cases are executed without exiting after satisfying the condition of 2.
If the above example is implemented as an if statement, it is as follows.

  1. <?php
  2. echo "Convert switch statement to if statement<br />";
  3. echo "<hr />";
  4.  
  5. $a = 3;
  6.  
  7. if($a == 0)
  8. echo "The variable $a is the number 0<br>";
  9. else if($a == 1)
  10. echo "The variable $a is the number 1<br>";
  11. else if($a == 2)
  12. echo "The variable $a is the number 2<br>";
  13. else if($a == 3)
  14. echo "The variable $a is the number 3<br>";
  15. else
  16. echo "0, 1, 2, 3 are not all.";
  17. ?>
php image

The following is the use of switch statement.

  1. <?php
  2. echo "Display month's date with switch statement<br /><hr />";
  3.  
  4. $mon = 2;
  5.  
  6. switch($mon) {
  7. case 1:
  8. case 3:
  9. case 5:
  10. case 7:
  11. case 8:
  12. case 10:
  13. case 12:
  14. echo "The {$mon} month has 31 days.<br />";
  15. break;
  16.  
  17. case 4:
  18. case 6:
  19. case 9:
  20. case 11:
  21. echo "Month {$mon} has 30 days<br />";
  22. break;
  23.  
  24. case 2:
  25. echo "{$mon} months have 28 days in the previous year.<br />";
  26. break;
  27.  
  28. default:
  29. echo "{$mon} is a number other than 1-12, representing the month.<br />";
  30. }
  31. ?>
php image

The switch statement can also be used as a condition such as big or small.
Like the following example.

  1. <?php
  2. echo "Put condition of switch statement<br /><hr />";
  3.  
  4. $age = 23;
  5.  
  6. switch($age) {
  7. case($age >= 10 && $age <= 19):
  8. echo "I am a teenager.";
  9. break;
  10.  
  11. case($age >= 20 && $age <= 29):
  12. echo "I'm in my twenties.";
  13. break;
  14.  
  15. case($age >= 30 && $age <= 39):
  16. echo "I am in my 30s.";
  17. break;
  18.  
  19. case($age >= 40 && $age <= 49):
  20. echo "I am in my 40s.";
  21. break;
  22.  
  23. case($age >= 50 && $age <= 59):
  24. echo "I am in my 50s.";
  25. break;
  26.  
  27. case($age >= 60 && $age <= 69):
  28. echo "I am in my 60s.";
  29. break;
  30.  
  31. case($age >= 70 && $age <= 79):
  32. echo "I am in my 70s.";
  33. break;
  34.  
  35. case($age >= 80 && $age <= 89):
  36. echo "I am in my 80s.";
  37. break;
  38.  
  39. case($age >= 90 && $age <= 99):
  40. echo "I am in my 90s.";
  41. break;
  42.  
  43. default:
  44. echo "you are baby PHPer";
  45. }
  46. ?>
php image