PHP Switch Case

PHP Switch Case is a if else condition and only different is the writing style, its mainly used when there needed a bulk amount of if and else if condition,

 

Writing Style of

PHP Switch Case

PHP Switch Case

Inside switch() function needed the value to pass and the conditions must have to written 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 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 case in 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 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 -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.