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

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

WBOY
Freigeben: 2016-06-13 11:09:22
Original
894 Leute haben es durchsucht

使用 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>
Nach dem Login kopieren

另外一个实例,利用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>
Nach dem Login kopieren

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


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage