PHP control statement-"switch" statement example

Source: Internet
Author: User
We have explained a multi-choice elseif statement. Although this statement can be used for multiple judgments, many if statements need to be written during use, which makes the readability of our program inconvenient, to avoid too many if statements and improve the readability of our program, you can use multiple switch judgment statements. in this chapter, we will introduce the "switch statement ". We have explained a multi-choice elseif statement. Although this statement can be used for multiple judgments, many if statements need to be written during use, which makes the readability of our program inconvenient, to avoid too many if statements and improve the readability of our program, you can use multiple switch judgment statements. in this chapter, we will introduce them to you. "Switch statement".

Switch statement syntax:

switch (expr){    case expr1:        statement;        break;      case expr2:        statement;        break;      ……    default:        statement;}

Syntax explanation:

The switch statement matches the expr1. in sequence with the expr1... if the comparison is not equal, continue to find the next case. if the comparison is equal, execute the corresponding PHP statement until the switch statement ends or a break is encountered. There is a default at the end of the switch statement. If no matching condition is found in all cases, the statement following the default statement is output, which is similar to the else statement.


Switch statement instance

In this example, multiple elseif statements are used first, and the switch statement described in this chapter is used again. Compare the two methods. You can see that writing is easier. The code is as follows:

Elseif statement writing

 

Switch statement syntax:

 

The running result is as follows:

The above two methods can be used to produce the same running Results. However, the elseif statements write a lot of if statements, and the code does not seem easy to read, switch statements are much more refined.

The above is a detailed description of the "switch" statement instance of the PHP control statement. For more information, see other related articles in the first PHP community!

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.