PHP Switch Case

PHP Switch Case

PHP Switch Case is an if-else condition and only difference is the writing style, it is mainly used when there needed a bulk amount of if and else if condition,

 

Writing Style of

PHP Switch Case

Inside switch() function needed the value to pass and the conditions must have to write inside the scope of the switch case or inside the switch function

then all the condition must have to written using case keyword and then space and then the desire equal value and after that have to give a colon and after the colon, the code should be started, and each case should be ended with a break statement.

if none of the given condition satisfied then have to execute a code then there is a keyword known by default like as PHP else condition, and after default have to give a colon and then the executable code, and the default should stay at the end of the PHP switch case.

if after all cases in the PHP switch case, we have not used the break statement then it will not act like PHP if else condition, it will go for check the next code of the switch case and it will execute the default case.

one example of a PHP switch case is given below.

Example:

<?php
$favcar = “Honda”;

switch ($favcolor) {
case “Maruti”:
echo “Your favorite car is Maruti!”;
break;
case “Honda”:
echo “Your favorite car is Honda!”;
break;
case “BMW”:
echo “Your favorite car is BMW!”;
break;
default:
echo “Your favorite car is neither Maruti, Honda, nor BMW!”;
}
?>

I hope you have learned PHP Switch Case and the post helped you,

Thanks for reading.

Share The Post On -