Home >Backend Development >PHP Tutorial >Application of PHP Switch statement in actual code_PHP tutorial
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>
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>
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.