Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

PHP Switch


PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.

Syntax

  1. switch(expression){
  2. case value1:
  3.  //code to be executed
  4.  break;
  5. case value2:
  6.  //code to be executed
  7.  break;
  8. ……
  9. default:
  10.  code to be executed if all cases are not matched;
  11. }

Flowchart

cswitch

Example

  1. <?php
  2. $num=20;
  3. switch($num){
  4. case 10:
  5. echo(“number is equals to 10”);
  6. break;
  7. case 20:
  8. echo(“number is equal to 20”);
  9. break;
  10. case 30:
  11. echo(“number is equal to 30”);
  12. break;
  13. default:
  14. echo(“number is not equal to 10, 20 or 30”);
  15. }
  16. ?>

Output:

number is equal to 20

Comments

comments