首页 > php教程 > php手册 > PHP Switch语句在实际代码中的应用

PHP Switch语句在实际代码中的应用

WBOY
发布: 2016-06-13 11:09:22
原创
894 人浏览过

使用 Switch 语句可以避免冗长的 if..elseif..else 代码块。

PHP Switch语句工作原理:

对表达式(通常是变量)进行一次计算
把表达式的值与结构中 case 的值进行比较
如果存在匹配,则执行与 case 关联的代码
代码执行后,break 语句阻止代码跳入下一个 case 中继续执行
如果没有 case 为真,则使用 default 语句
 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li>
<span>switch ($</span><span class="attribute">d</span><span>=</span><span class="attribute-value">date</span><span>("D"))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li><span>case "Mon";  </span></li>
<li class="alt"><span>echo "周一";  </span></li>
<li><span>break;  </span></li>
<li class="alt"><span>case "Tue";  </span></li>
<li><span>echo "周二";  </span></li>
<li class="alt"><span>break;  </span></li>
<li><span>case "Wed";  </span></li>
<li class="alt"><span>echo "周三";  </span></li>
<li><span>break;  </span></li>
<li class="alt"><span>case "Thu";  </span></li>
<li><span>echo "周四";  </span></li>
<li class="alt"><span>break;  </span></li>
<li><span>case "Fir";  </span></li>
<li class="alt"><span>echo "周五";  </span></li>
<li><span>break;  </span></li>
<li class="alt"><span>case "Sat";  </span></li>
<li><span>echo "周六";  </span></li>
<li class="alt"><span>break;  </span></li>
<li><span>case "Sun";  </span></li>
<li class="alt"><span>echo "周日";  </span></li>
<li><span>break;  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
登录后复制

另外一个实例,利用PHP Switch语句实现一个页面多用途,首先建立test.php页面:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">'solution.php?<br>action=add'</span><span class="tag">></span><span>增加</span><span class="tag"><span> /a</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span></span></span></span></span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">'solution.php?<br>action=del'</span><span class="tag">></span><span>删除</span><span class="tag"><span> /a</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span></span></span></span></span>
</li>
<li>
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">'solution.php?<br>action=search'</span><span class="tag">></span><span>查找</span><span class="tag"><span> /a</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span></span></span></span></span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">'solution.php?<br>action=update'</span><span class="tag">></span><span>更新</span><span class="tag"><span> /a</span><span class="tag">></span><span>";  </span></span></span>
</li>
<li>
<span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
</ol>
登录后复制

当然PHP Switch语句这里sunec省略了其他大部分的代码,只是把其中一些提交按钮写了出来。我们可以看到,无论点击哪个按钮,我们都会跳转到solution.php页面,唯一不不同的是,问号后的action的内容分成了4种。我们可以把它称作是一种提示。


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板