Wednesday, 17 June 2020

php switch and php nested switch

  1. using swithc we can get the required , if u see , whenever u appears, in case that will print and at that breaks because of break


  2. <?php      
        $ch = 'U';  
        switch ($ch)  
        {     
            case 'a':   
                echo "Given character is vowel";  
                break;  
            case 'e':   
                echo "Given character is vowel";  
                break;  
            case 'i':   
                echo "Given character is vowel";  
                break;  
            case 'o':   
                echo "Given character is vowel";  
                break;    
            case 'u':   
                echo "Given character is vowel";  
                break;  
            case 'A':   
                echo "Given character is vowel";  
                break;  
            case 'E':   
                echo "Given character is vowel";  
                break;  
            case 'I':   
                echo "Given character is vowel";  
                break;  
            case 'O':   
                echo "Given character is vowel";  
                break;  
            case 'U':   
                echo "Given character is vowel";  
                break;  
            default:   
                echo "Given character is consonant";  
                break;  
        }  
    ?>    





php fall through




<?php      
    $ch = 'c';  
    switch ($ch)  
    {     
        case 'a':   
            echo "Choice a";  
            break;  
        case 'b':   
            echo "Choice b";  
            break;  
        case 'c':   
            echo "Choice c";      
            echo "</br>";  
        case 'd':   
            echo "Choice d";  
            echo "</br>";  
        default:   
            echo "case a, b, c, and d is not found";  
    }  
?>    


Choice c
Choice d
case a, b, c, and d is not found



php nested swithch statement




<?php      
    $car = "Hyundai";                   
        $model = "Tucson";    
        switch( $car )    
        {    
            case "Honda":    
                switch( $model )     
                {    
                    case "Amaze":    
                           echo "Honda Amaze price is 5.93 - 9.79 Lakh.";   
                        break;    
                    case "City":    
                           echo "Honda City price is 9.91 - 14.31 Lakh.";    
                        break;     
                }    
                break;    
            case "Renault":    
                switch( $model )     
                {    
                    case "Duster":    
                        echo "Renault Duster price is 9.15 - 14.83 L.";  
                        break;    
                    case "Kwid":    
                           echo "Renault Kwid price is 3.15 - 5.44 L.";  
                        break;    
                }    
                break;    
            case "Hyundai":    
                switch( $model )     
                {    
                    case "Creta":    
                        echo "Hyundai Creta price is 11.42 - 18.73 L.";  
                        break;    
        case "Tucson":    
                           echo "Hyundai Tucson price is 22.39 - 32.07 L.";  
                        break;   
                    case "Xcent":    
                           echo "Hyundai Xcent price is 6.5 - 10.05 L.";  
                        break;    
                }    
                break;     
        }  
?>    
Hyundai Tucson price is 22.39 - 32.07 L




PHP Break: switch statement without break




<?php  
$car = 'Mercedes Benz';  
switch ($car) {    
default:  
echo '$car is not Mercedes Benz<br>';  
case 'Orange':  
echo '$car is Mercedes Benz';  
}  
?>  


$car is not Mercedes Benz
$car is Mercedes Benz


PHP Break: using optional argument

<?php  
$i = 0;  
while (++$i) {  
    switch ($i) {  
        case 5:  
            echo "At matched condition i = 5<br />\n";  
            break 1;  // Exit only from the switch.   
       case 10:  
            echo "At matched condition i = 10; quitting<br />\n";  
            break 2;  // Exit from the switch and the while.   
       default:  
            break;  
    }  
}?>  

output
At matched condition i = 5
At matched condition i = 10; quitting







No comments:

Post a Comment