本文章详细的介绍了switch语句在php中用法,以及switch case在条件为0时的处理办法,有需要了解的同学可以参考一下.
Switch 语句
如果您希望有选择地执行若干代码块之一,请使用 Switch 语句.
使用 Switch 语句可以避免冗长的 if..elseif..else 代码块.
实例代码如下:
- switch (expression)
- {
- case label1:
- code to be executed if expression = label1;
- break;
- case label2:
- code to be executed if expression = label2;
- break;
- default:
- code to be executed
- if expression is different
- from both label1 and label2;
- }
实例
工作原理:
对表达式(通常是变量)进行一次计算
把表达式的值与结构中 case 的值进行比较
如果存在匹配,则执行与 case 关联的代码
代码执行后,break 语句阻止代码跳入下一个 case 中继续执行
如果没有 case 为真,则使用 default 语句
实例代码如下:
- switch ($cps_sign) {
- case 'yiqifa':
- case 'chengguo':
- case 'roiyiqifa':
- case 'lkt':
- case 'fanli':
- case 'qqfanli':
- case 'weiyi':
- case 'yoyi':
- $sql = "INSERT into sa_cps_list (`uv`,`s_time`,`cps`,`url`) VALUES ('{$uv}',{$timestamp},'{$cps_sign}','{$url}')";
- echo $sql;exit();
- mysql_query($sql);
- break;
- default:
- break;
- }
仔细看了一下程序,莫非是switch和case惹的祸?于是,写DEMO检测.
//输出结果为:bool(true) bool(false) xxx
实例代码如下:
- var_dump(""==0);
- var_dump(""===0);
- $errid = '';
- switch ($errid) {
- case 0 :
- echo "xxx";
- break;
- default:
- echo "yyy";
- }
真相大白了,原来 switch/case结构中,比较case值用的是==而不是===.这样,空就和0相等了,我收到的结果也当然就是错误的了.
没办法,程序不想改,毕竟多条件的时候switch/case要比一堆if让人看着舒心.想办法吧.呵呵.
实例代码如下:
- $result = '';
- if(is_numeric($err_id) === false) {
- $result .= '宕机或超时,没有返回值';
- return $result;
- }
- switch ($err_id) {
- case xxx :
- ……….
- }
问题解决了,先判断一下返回值是否是数值,如果不是数值的话,直接return.
以后写switch/case判断数字的时候一定要小心,尤其是有0这个败家值存在的时候
switch 语句和 elseif 语句的比较
在switch 语句中,条件只求一次,然后和每个 case 比较,而在 elseif 语句中,条件会再次求值.如果你的条件比较复杂,或者是多次循环,用 switch 语句速度会更加快些
波比源码 » php中switch语句的用法介绍
order levofloxacin 500mg online levaquin 500mg tablet
order avodart 0.5mg online cheap celecoxib sale zofran 4mg pill
purchase tadalafil online order avanafil 100mg order voltaren 50mg online cheap
tadalafil 20mg comprimГ© acheter 50mg du viagra sildenafil 25mg generique
purchase modafinil provigil uk buy acetazolamide 250mg pill
imuran 100 mcg usa viagra 50mg viagra 100mg price
cialis 10mg ca cialis 20mg pills sildenafil 50mg pills
tadalafil cost buy cialis 10mg order generic amantadine
piracetam 800 mg canada canadian viagra oral viagra
buy generic chloroquine buy cenforce 50mg online cheap buy olumiant 2mg without prescription
glycomet 500mg canada lipitor 40mg us tadalafil 20mg for sale
buy linezolid 600 mg slot casino casino online games
purchase essays online blackjack poker online win real money online casino for free
buy vardenafil 20mg sale order levitra 20mg pills methylprednisolone 4mg over the counter
buy custom research papers term papers for sale online purchase viagra for sale
cheapest cialis online cheap sildenafil 100mg viagra 100mg for sale
orlistat online purchase diltiazem pills buy acyclovir 400mg online cheap
order tadalafil 20mg online buy inderal 10mg online plavix 75mg oral
order allopurinol 100mg sale purchase ezetimibe generic order generic ezetimibe 10mg
order domperidone 10mg sale buy generic flexeril 15mg buy flexeril 15mg pill
ozobax cheap buy baclofen 10mg for sale ketorolac without prescription
ranitidine 300mg canada meloxicam for sale online celecoxib 100mg brand
online blackjack gambling buy tadalafil 20mg online buy tadalafil 10mg generic
trazodone brand sildenafil price order aurogra pill
sildenafil buy online buy tamoxifen sale rhinocort nasal spray
deltasone ca order vermox 100mg sale buy vermox generic