Skip to main content

//Switch Case (Integer)

Switch Case (Integer)

  1. <?php 
  2. //Switch Integer:- In Switch Integer we use Numbers(integers)

  3. $a = 30; //variable def
  4. $b = 20;
  5. $c;
  6. $int = '1'; //which case you want to execute 'Enter' case value.

  7. switch($int) 
  8. {
  9. case '1': //the case label must 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 '2': //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 '3':
  24. {
  25. //Multiplication
  26. $c = $a * $b;
  27. echo "the mulitiplication of a+b is:".$c."<br>";
  28. break;
  29. }

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

  37. case '5':
  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