Skip to main content

//Switch Case (String)

 Switch Case (String)


  1. <?php 
  2. //Switch String:- In Switch String we use words and numbers in "duble quotes".

  3. $a = 30;  //Variable Def
  4. $b = 20;
  5. $c;
  6. $string = 'five'; //which case you want to execute 'Enter' case. value.

  7. switch($string) 
  8. {
  9. case 'one': //the case label must be end with colon(:)
  10. {
  11. //Addition
  12. $c = $a + $b;
  13. echo "the addition of a+b is:".$c."<br>"; //"echo" use for print line.
  14. break; //It's necessary to use break after each block.
  15. }

  16. case 'two': //case label must be unique.
  17. {
  18. //Subtract
  19. $c = $a - $b;
  20. echo "the subtraction of a-b is:".$c."<br>"; //"<br>" use for linebreak.
  21. break; //if you don't use it, then all cases executed.
  22. }

  23. case 'three':
  24. {
  25. //Multiplication
  26. $c = $a * $b;
  27. echo "the mulitiplication of a*b is:".$c."<br>";
  28. break;
  29. }

  30. case 'four':
  31. {
  32. //Division
  33. $c = $a / $b;
  34. echo "the division of a / b  is:".$c."<br>";
  35. break;
  36. }

  37. case 'five':
  38. {
  39. //Modulus
  40. $c = $a % $b;
  41. echo "the modulos of a % b is:".$c."<br>";
  42. break;
  43. }


  44.         default: //If none of the case label values matches to the value of the expression, then default part statement will be executed.
  45. echo '<i style="color:red ;"> Enter the valid value </i>';
  46. break;
  47. }
  48.  ?>


👉Execute👈



//Output

/*

a = 30; b = 20;


case +: c = a + b;

the value of a+b is:50


case -: c = a - b;

the value of a-b is:10


case /: c = a / b;

the value of a/b is:1


case *: c = a * b;

the value of a*b is:600


case %: c = a % b;

the value of a%b is:10

*/






                                                                     //ThE ProFessoR