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

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

WBOY
Release: 2016-06-13 11:09:22
Original
893 people have browsed it

使用 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>
Copy after login

另外一个实例,利用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>
Copy after login

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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template