Intro:The switch-case statement in php

To be continued,the switch-case statement comes.
An alternative to the if -elseif-else statement is the switch-case statement,which does almost the same thing:it tests a variable against a series of values until if finds a match,and then executes the code corresponding to that match.Consider the following code listing,which is equivalent to the preceding one:
<?php
//handle multiple possibilities
//define a different message for each day
$today='Tuesday';
switch($today){
 case 'Monday':
   echo 'Monday/'s child is fair of face.';
   break;
case 'Tuesday':
   echo 'Tuesday/'s child is full of grace.';
   break;
case 'Wednesday':
  echo 'Wednesday/'s child is full of woe.';
  break;
case '':......
default:
   echo 'No information available for that day';
   break;
}
?>
--------------------------------------------------------------------------------
The switch-case construct differs from the if-elseif-else construct in one important way.Once PHP finds a case statement that evaluates to true,it executes not only the code corresponding to that case statement,but also the code for all subsequent case statements.If this is not what you want,add a break statement to the end of each case block(as is done in the previous listing)to tell PHP to break out of the switch-case statement block once it executes the code corresponding to the first true case.
Notice also the 'default' case:as the name suggests,this specifies the default set of actions PHP should take if none of the other cases evaluate to true.This default case,like the else branch of the if-elseif-else block,is very useful as a "catch-all" handler for unforeseen situations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值