Switch Statement in PHP with Examples

Hello Friend how are you i hope you all are doing well. Today in this post we will learn about switch statement in PHP like what is switch statement, syntax of switch statement and some programs with examples. If you want to learn PHP in very simple easy way then this post will help you definitely.

Let's Start

1.Switch statement allows us to execute one statement from many statement and the statements are called case.
2.Inside the body of switch there are a number of cases and there is a single number is passed at the place of parameter to select and execute a case.

1.In the switch statement a value/number is passed in the place of parameter and the case from which the parameter is matched is executed.
2.If no case matched with parameter then default case will execute.
Let's try to understand using example:

<?php
//variable definition
$day=2;
switch($day)
{
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
case 4:
echo "Thrusday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday";
break;
case 7:
echo "Sunday";
break;
default:
echo "No case matched";
}
?>
//output
Tuesday
because day is equal to 2 and it matches with
case 2 so the output is Tuesday

Program: Check given Alphabet is vowel or consonant.

 
<?php
//variable definition
$ch='O';
switch($ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
echo "vowel";
break;
default:
echo "Consonant";
}
?>
//output
Vowel

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

0 Comments