Use the Switch statement to avoid lengthy if. ElseIf. else code block.
How the PHP switch statement works:
Perform a single calculation on an expression (usually a variable)
Compare the value of an expression with the value of a case in the structure
If there is a match, the code associated with the case is executed
After the code executes, the break statement prevents the code from jumping into the next case to continue execution
If no case is true, use the default statement
- < ? PHP
- switch ($d=date("D"))
- {
- Case "Mon";
- echo "Monday";
- Break
- Case "Tue";
- echo "Tuesday";
- Break
- Case "Wed";
- echo "Wednesday";
- Break
- Case "Thu";
- echo "Thursday";
- Break
- Case "Fir";
- echo "Friday";
- Break
- Case "Sat";
- echo "Saturday";
- Break
- Case "Sun";
- echo "Sunday";
- Break
- }
- ?>
Another example, using the PHP switch statement to achieve a page multi-purpose, first set up the test.php page:
- < ? PHP
- echo " < a href=' solution.php?
Action=add '> Add < /A>< br> < br>";
- echo " < a href = ' solution.php?
Action=del ' > Delete < /a span class= "tag" >> < BR > < br > ";
- echo " < a href=' solution.php?
Action=search '> find < /A>< br> < br>";
- echo " < a href=' solution.php?
Action=update '> update < /A>';
- ?>
Of course the PHP switch statement here Sunec omitted most of the other code, just to write some of the commit buttons. We can see that no matter which button we click, 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 kinds. We can call it a hint.
http://www.bkjia.com/PHPjc/446158.html www.bkjia.com true http://www.bkjia.com/PHPjc/446158.html techarticle use the Switch statement to avoid lengthy if. ElseIf. else code block. The PHP switch statement works by calculating the value and structure of an expression (usually a variable) at once .