Home > Backend Development > PHP Tutorial > Application of PHP Switch statement in actual code_PHP tutorial

Application of PHP Switch statement in actual code_PHP tutorial

WBOY
Release: 2016-07-15 13:32:19
Original
975 people have browsed it

Use Switch statements to avoid lengthy if..elseif..else blocks.

How the PHP Switch statement works:

Perform a calculation on an expression (usually a variable)
Compare the value of the expression with the value of the case in the structure Compare
If there is a match, execute the code associated with the case
After the code is executed, the break statement prevents the code from jumping to the next case to continue execution
If no case is true, use the default statement

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php   </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></span></li></ol>
Copy after login

Another example, using the PHP Switch statement to achieve a multi-purpose page, first create the test.php page:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>echo "</span><span class="tag"><</span><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><span> /a</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span></span></li>
<li class="alt">
<span>echo "</span><span class="tag"><</span><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><span> /a</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span>
</li>
<li>
<span>echo "</span><span class="tag"><</span><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><span> /a</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";  </span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><</span><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><span> /a</span><span class="tag">></span><span>";  </span>
</li>
<li>
<span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
</ol>
Copy after login

Of course, sunec omits most of the other codes in the PHP Switch statement, and just writes some of the submit buttons. We can see that no matter which button is clicked, we will jump to the solution.php page. The only difference is that the content of the action after the question mark is divided into 4 types. We can call it a reminder.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446158.htmlTechArticleUse Switch statement to avoid lengthy if..elseif..else code blocks. PHP Switch statement works: Perform a calculation on an expression (usually a variable) and combine the value and structure of the expression...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template