PHP switch Statement switch case Learn notes

Source: Internet
Author: User
Tags php switch switch case

Switch statements in PHP are used to perform different actions based on multiple different conditions. If you want to selectively execute one of several blocks of code, use a Switch statement.

Switch syntax

The code is as follows Copy Code

switch (expression)//Set expression
{
Case Label1://Compare the value of label 1
Code to is executed if expression = Label1; If matching the output associated with the code
Break End code Jump statement
Case LABEL2://Compare the value of label 1
Code to is executed if expression = Label2;
Break
Default://If no match, execute defaults
Code to IS executed
If expression is different
From both Label1 and Label2;
}

Working principle:

Evaluate an expression (usually a variable) once
Compare the value of an expression with the value of a case in a structure
If there is a match, the code associated with the case is executed
After the code executes, the break statement blocks the code from jumping into the next case to continue execution
If no case is true, the default statement is used

The code is as follows Copy Code

<?php
$x = 1; The variable x is assigned a value of 4
Switch ($x)
{
Case 1://The value to be compared is 1
echo "Number 1";
Break
Case 2://The value to be compared is 2
echo "Number 2";
Break
Case 3://The value to be compared is 3
echo "Number 3";
Break
Default://No match is matched, output defaults
echo "does not have a number from 1 to 3";
}
?>

Learning experience, SWICTH case is just a switch statement, if there are many conditions or a variety of circumstances we can not use it, then we need to use if else to operate.

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.