Usage examples of php switch statement

WBOY
Release: 2016-07-25 08:59:13
Original
1301 people have browsed it
  1. /**
  2. * Simple example of php switch
  3. * edit bbs.it-home.org
  4. */
  5. $i=1;
  6. switch ($i){
  7. case 0:
  8. echo "is equles 0";
  9. break;
  10. case 1:
  11. echo "is equles 1";
  12. break;
  13. case 2:
  14. echo "is equles 2";
  15. break;
  16. default:
  17. echo "this is default";
  18. }
  19. ?>
Copy code

The following is a detailed description of the Switch statement in php.

When you need to selectively execute one of several code blocks, consider using the Switch statement. Use a Switch statement to avoid lengthy if..elseif..else blocks.

How it works:

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

Example 2 , switch to achieve multiple uses of one page.

First, create the test.php page:

  1. echo "add

    ";
  2. echo "< a href='solution.php?action=del'>delete

    ";
  3. echo "find

    ";
  4. echo "Update";
  5. //edit bbs.it- home.org?>
Copy code

Note: here sunec omits most of the other code and just writes some of the submit buttons. No matter which button is clicked, it 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. Call it a reminder.

Then, create the solution.php page to handle these four operations.

  1. $action=$_GET["action"];
  2. switch ($action)
  3. {
  4. case "add":
  5. echo "Added functions can now be implemented!";
  6. break;
  7. case "del":
  8. echo "The deletion function can now be implemented!";
  9. break;
  10. case "search":
  11. echo "The query function can now be implemented!";
  12. break;
  13. case "update":
  14. echo "Now The update function can be realized! ";
  15. break;
  16. }
  17. ?>
Copy code

Code description: After receiving the value of action, use the switch statement to assign corresponding operations to it according to the different action values. It's much better than using if else statement.

Let’s introduce these, examples of PHP switch statements, I hope it will be helpful to friends who are learning PHP.



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!