Rumah >pembangunan bahagian belakang >tutorial php >PHP Switch语句在实际代码中的应用_PHP教程

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

WBOY
WBOYasal
2016-07-15 13:32:19976semak imbas

使用 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种。我们可以把它称作是一种提示。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446158.htmlTechArticle使用 Switch 语句可以避免冗长的 if..elseif..else 代码块。 PHP Switch语句工作原理: 对表达式(通常是变量)进行一次计算 把表达式的值与结构...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn