11. PHP Tutorial _php Switch statement

Source: Internet
Author: User

The switch statement is used to perform different actions based on several different conditions.

PHP Switch Statement

If you want to selectively execute one of several blocks of code , use the switch statement.

Grammar
1 Switch(n)2  {3   CaseLabel1:4If n=Label1, here the code will be executed;5  Break;6   CaseLabel2:7If n=Label2, here the code will be executed;8  Break;9  default:Ten if n is neither equal to Label1 nor equal to Label2, the code will be executed here; One}

How it works: first evaluate a simple expression n(usually a variable). Compares the value of an expression to the value of each case in the structure. If there is a match,

The code associated with the case is executed. After the code executes, use break to prevent the code from jumping into the next case to continue execution. The default statement is used for nonexistent matches (that is, no

Case is true) is executed.

1<?PHP2  $favcolor= "Red";3  Switch($favcolor)4  {5   Case"Red":6  Echo"Your favorite color is red!";7   Break;8   Case"Blue":9  Echo"Your favorite color is blue!";Ten   Break; One   Case"Green": A  Echo"Your favorite color is green!"; -   Break; -  default: the  Echo"Your favorite color is neither red, blue, or green!"; -  } -?>

11. PHP Tutorial _php Switch statement

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.