Course Banner


PHP Switch Case

If you have more than one task to perform based on different condition then we use switch case.

Use the switch statement to select one of many blocks of code to be executed.

Example :

  1. $day = 1;
  2. switch ($day) {
  3. case1:
  4.       echo"This is Sunday";
  5.       break;
  6. case2:
  7.       echo"This is Monday!";
  8.       break;
  9. case3:
  10.       echo"This is Tuesday";
  11.       break;
  12. case3:
  13.       echo"This is Wednesday!";
  14.       break;
  15. case3:
  16.       echo"This is Thursday!";
  17.       break;
  18. case3:
  19.       echo"This is Friday!";
  20.       break;
  21. case3:
  22.       echo"This is Saturday";
  23.       break;
  24. default:
  25.       echo"You have selected wrong day!";
  26. }
  27. ?>

Output :

What will happen if we don’t write ‘Break’?

If we don’t write break, it will show matching statement as well as all other statements which is not good. We want the program to show the matching statement and then break the program.