PHP control statement Switch_PHP tutorial

Source: Internet
Author: User
Switch of PHP control statements. 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 swi 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.

<?phpswitch ($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";}?>


Http://www.bkjia.com/PHPjc/446733.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446733.htmlTechArticleSwitch statements use Switch statements if you want to selectively execute one of several code blocks. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else. Syntax swi...

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.