Switch Statement with output in php

Switch Statement with output in php
Switch Statement with output in php

How to use a switch statement with output in PHP

Switch statement: Switch statement allows us to execute one statement from many statements and the statements are called cases.

The PHP switch statement is used to execute one statement from multiple conditions. It works like a PHP if-else-if statement. Inside the body of the switch, there are some cases and there is a single number is passed at the place of the parameter to select and execute a case.


Some Others Programming Language Switch Statement 

👉C Switch Statement 

👉Java Switch Statement


Some Important Points To Be Switch Case in PHP:

1. The default is an optional statement. Even it is not important, the default must always be the last statement.

2. There can be only one default in a switch statement. More than one default may lead to a Fatal error.

3. Each case can have a break statement, which is used to terminate the sequence of statements.

4. The break statement is optional to use in the switch. If the break is not used, all the statements will execute after finding matched case values.

5. PHP allows you to use numbers, characters, strings, as well as functions in switch expressions.

6. Nesting of switch statements is allowed, but it makes the program more complex and less readable.


Switch Statement Syntax in PHP

switch(expression)

{      

case value1:      

 //code to be executed  

 break;  

case value2:      

 //code to be executed  

 break;  

......      

default:       

 code to be executed if all cases are not matched;    

}  

Switch statement a value and number are passed in the place of parameter and the case from which the parameter is matched is executed. If no case matched with the parameter then the default case will execute.


How To Use Switch Statement Week Program in PHP?

<?php

 $day=5;

switch($day)

{

case 1:

echo "Monday";

break;

case 2:

echo "Tuesday";

break;

case 3:

echo "Wednesday";

break;

case 4:

echo "Thursday";

break;

case 5:

echo "Friday";

break;

case 6:

echo "Saturday";

break;

case 7:

echo "Sunday";

break;

default:

echo "No case matched";

}

?>

*****OUTPUT*****

Friday


How To Check Given Alphabet is vowel or Consonant in PHP?

<?PHP      

$ch = 'O';  

switch ($ch)

{     

case 'a':   

echo "Given character is vowel";  

break;  


case 'e':   

echo "Given character is vowel";  

break;  

case 'i':   

echo "Given character is 
the vowel";  

break;  

case 'o':   

echo "Given character is the vowel";  

break;    

case ' u ':   

echo "Given character is the vowel";  

break;  

case 'A':   

echo "Given character is the vowel";  

break;  

case 'E':   

echo "Given character is vowel";  

break;  

case 'I':   

 echo "Given character is the vowel";  

break;  

case 'O':   

echo "Given character is the vowel";  

break;  

case 'U':   

echo "Given character is vowel";  

break;  

default:   

echo "Given character is consonant";  

break;  

}  

?>  

*****OUTPUT*****

The given character is the vowel


How To Use Number Switch Statement in PHP?

<?PHP      

$num=30;      

switch($num)

{      

case 10:      

echo(" The Number is equals to 10");      

break;      

case 20:      

echo(" The Number is equal to 20");      

break;      

case 30:      

echo(" The Number is equal to 30");      

break;      

default:      

echo("Number is not equal to 10, 20 or 30");      

}     

?>  

*****OUTPUT*****

 The Number is equal to 30


How To Use Switch Statement with String in PHP?

<?PHP      

$ch = "B.Tech";  

switch ($ch)  

{     

case "B.C.A":   

echo "B.C.A is 3 Years Course";  

break;  

case "B.S.C":   

echo "B.S.C is 3 Years Course";  

break;  

case "B.Tech":   

echo "B.Tech is 4 Years Course";  

break;  

case "B.Arch":   

echo "B.Arch is 5 Years Course";  

break;  

default:   

echo "Wrong Choice";  

break;  

}  

?>    

*****OUTPUT*****

B.Tech is 4 Years Course


Nested Switch Statement: A Nested switch statement means switch statement inside another switch statement in PHP.


How To Use Nested Switch Statement in PHP?

<?PHP      

$car = "Hyundai";                   

$model = "Tucson";    

switch( $car )    

{    

case "Honda":    

switch( $model )     

{    

case "Amaze":    

echo "Honda Amaze";   

break;    

case "City":    

echo "Honda City";    

break;     

}    

break;    

case "Renault":    

switch( $model )     

{    

case "Duster":    

echo "Renault Duster";  

break;    

case "Kwid":    

echo "Renault Kwid";  

 break;    

 }    

break;    

case "Hyundai":    

switch( $model )     

{    

case "Creta":    

echo "Hyundai Creta";  

break;    

case "Tucson":    

echo "Hyundai Tucson";  

break;   

case "Xcent":    

echo "Hyundai Xcent";  

break;    

}    

break;     

}  

?>   

*****OUTPUT*****

Hyundai Tucson's


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.