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 »
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.
The break
keyword.
The case
keyword.
The default
keyword.
Read more about switch conditionals in our PHP switch Statement Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!