PHP Switch Statement

Source: Internet
Author: User
Tags php switch

PhpSwitch Statement

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

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

Grammar
<?php
Switch (n)
{
Case Label1:
If N=label1, the code here will be executed;
Break
Case LABEL2:
If N=label2, the code here will be executed;
Break
Default
If n is neither equal to Label1 nor equal to Label2, the code will be executed here;
}
?>

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 when no match exists (that is, no case is true) when executed.

Instance
<?php
$favcolor = "Red";
Switch ($favcolor)
{
Case "Red":
echo "The color you like is red!";
Break
Case "Blue":
echo "The color you like IS blue!";
Break
Case "Green":
echo "The color you like IS green!";
Break
Default
echo "The color you like is not red, blue, or green!";
}
?>
The output is: The color you like is red!

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.