如果您希望有选择地执行若干代码块之一,请使用Switch语句,使用Switch语句可以避免冗长的if..elseif..else代码块。
语法
switch(expression)
{
case label1:
expression = label1时执行的代码;
break;
case label2:
expression = label2时执行的代码;
break;
default:
表达式的值不等于label1及label2时执行的代码;
}
实例
<?PHP
$favfruit="orange";
switch($favfruit){
case "apple":
echo "Your favorite fruit is apple!";
break;
case "banana":
echo "Your favorite fruit is banana!";
break;
case "orange":
echo "Your favorite fruit is orange!";
break;
default:
echo "Your favorite fruit is neither apple, banana, or orange!";
}
?>
本文来自投稿,不代表本站立场,如若转载,请注明出处:https://www.xiaobinblog.com/1075.html
发表评论