Example of Switch statement usage in PHP

Source: Internet
Author: User
The Switch statement in PHP is used to execute different actions based on multiple different conditions. Switch statement if you want to selectively execute one of several code blocks, use the Switch statement. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else. Syntax switch (expression) {caselabel1: codetobeexe

The Switch statement in PHP is used to execute different actions based on multiple different conditions. Switch statement if you want to selectively execute one of several code blocks, use the Switch statement. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else. Syntax switch (expression) {case label1: code to be exe

The Switch statement in PHP is used to execute different actions based on multiple different conditions.

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

Use the Switch statement to avoid lengthy code blocks such as if... elseif... else.

Syntax
Switch (expression)
{
Case label1:
Code to be executed if expression = label1;
Break;
Case label2:
Code to be executed if expression = label2;
Break;
Default:
Code to be executed
If expression is different
From both label1 and label2;
} Instance

Working principle:

Perform a calculation on the expression (usually a variable ).
Compare the expression value with the case value in the Structure
If a match exists, execute the code associated with the case
After the code is executed, the break statement prevents the code from jumping into the next case for further execution.
If no case is true, use the default statement.
Switch ($ x)
{
Case 1:
Echo "Number 1 ";
Break;
Case 2:
Echo "Number 2 ";
Break;
Case 3:
Echo "Number 3 ";
Break;
Default:
Echo "No number between 1 and 3 ";
}
?>

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.