Showing posts with label php switch statement. Show all posts
Showing posts with label php switch statement. Show all posts

Wednesday, April 3, 2024

PHP switch statement

 switch statement in PHP

PHP switch statement


1] switch case is use in menu driven programming.

2] The value of the switch expression is calculated once's then this
    value is match  which case in switch block---- if there is match
    associated case is executed.

3] use break keyword to transfer keyword outside they switch block
    after case execution.

4] switch case value is the expression is case sensitive.

5] The switch statement in PHP is an effective construct that lets you carry out various operations depending on various circumstances.
 
6]  It's especially helpful when you have to assess several scenarios and run various code blocks according to the first real situation that appears. Now let's see how it functions.

      Syntax :-

statement 
switch (expression/value)
{
  case value:  
 statement;
  statement;
     break; 

  case value: 
 statement;
 statement;
    break;

      default :   
 statement;
 statement;
}         


PHP switch statement 
    
    Program

<?php
$no1=20;
$no2=5;
$choice=4;
switch($choice)
 {
    case 1 : $result =$no1+$no2;
     echo"Addition =".$result;
     break;
     case 2 : $result =$no1-$no2;
     echo"Substraction =".$result;
     break;
     case 3 : $result =$no1*$no2;
     echo"Multiplication =".$result;
     break;
     case 4 : $result =$no1/$no2;
     echo"division =".$result;
     break;
     default : echo "wrong choice";
 }
?>

PHP switch statement 
Output :- 

PHP switch statement  Example /output



Read more