PHP switch Keyword
Example
Choose a message to display based on the value of a variable:
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color
is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
Try it Yourself »
Definition and Usage
The switch
keyword is used to create a switch conditional.
Switch conditionals choose a block of code to run based on the value of an expression.
Related Pages
The break
keyword.
The case
keyword.
The default
keyword.
Read more about switch conditionals in our PHP switch Statement Tutorial.
❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.