Application of PHP switch statement in actual code _php tutorial

Source: Internet
Author: User
Tags php switch

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

 
 
  1. < ? PHP
  2. switch ($d=date("D"))
  3. {
  4. Case "Mon";
  5. echo "Monday";
  6. Break
  7. Case "Tue";
  8. echo "Tuesday";
  9. Break
  10. Case "Wed";
  11. echo "Wednesday";
  12. Break
  13. Case "Thu";
  14. echo "Thursday";
  15. Break
  16. Case "Fir";
  17. echo "Friday";
  18. Break
  19. Case "Sat";
  20. echo "Saturday";
  21. Break
  22. Case "Sun";
  23. echo "Sunday";
  24. Break
  25. }
  26. ?>

Another example, using the PHP switch statement to achieve a page multi-purpose, first set up the test.php page:

 
 
  1. < ? PHP
  2. echo " < a href=' solution.php?
    Action=add '> Add < /A>< br> < br>";
  3. echo " < a href = ' solution.php?
    Action=del ' > Delete < /a span class= "tag" >> < BR > < br > ";
  4. echo " < a href=' solution.php?
    Action=search '> find < /A>< br> < br>";
  5. echo " < a href=' solution.php?
    Action=update '> update < /A>';
  6. ?>

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 .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.