Introduction to the use of switch statements in PHP _php tutorial

Source: Internet
Author: User
Tags switch case
This article describes in detail the use of the switch statement in PHP, as well as the switch case in the condition of 0 o'clock processing method, there is a need to understand the students can refer to.

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

Use the Switch statement to avoid lengthy if. ElseIf. else code block.

Grammar

The code is as follows Copy Code
switch (expression)
{
Case Label1:
Code to is executed if expression = Label1;
Break
Case LABEL2:
Code to is executed if expression = Label2;
Break
Default
Code to be executed
If expression is different
From both Label1 and Label2;
}

Instance
Working principle:

Perform a single calculation on an expression (usually a variable)
Compare the value of an expression with the value of a case in the structure
If there is a match, the code associated with the case is executed
After the code executes, the break statement prevents the code from jumping into the next case to continue execution
If no case is true, use the default statement

The code is as follows Copy Code

Switch ($cps _sign) {
Case ' Yiqifa ':
Case ' Chengguo ':
Case ' Roiyiqifa ':
Case ' LKT ':
Case ' Fanli ':
Case ' Qqfanli ':
Case ' Weiyi ':
Case ' Yoyi ':
$sql = "INSERT into sa_cps_list (' UV ', ' s_time ', ' cps ', ' url ') VALUES (' {$uv} ', {$timestamp}, ' {$cps _sign} ', ' {$url} ')";
echo $sql; exit ();
mysql_query ($sql);
Break
Default
Break
}


Have a closer look at the procedure, is it a switch and case trouble? So, write demo test.
The output is: BOOL (TRUE) bool (false) XXX

The code is as follows Copy Code

Var_dump ("==0");
Var_dump ("===0");

$errid = ";
Switch ($errid) {
Case 0:
echo "XXX";
Break
Default
echo "YYY";
}

The truth, the original switch/case structure, compare case value is = = instead of = = =. Thus, the emptiness is equal to 0, and the result I receive is of course the wrong one.
No way, the program does not want to change, after all, many conditions when the switch/case than a bunch of if let people look comfortable. Find a way to do it. Oh.

The code is as follows Copy Code

$result = ";
if (Is_numeric ($err _id) = = = False) {
$result. = ' outage or timeout, no return value ';
return $result;
}
Switch ($err _id) {
Case XXX:
..........
}

Problem solved, first determine whether the return value is a value, if not a value, direct return.
You must be careful when writing switch/case to judge numbers, especially when there are 0 of these prodigal values.

Comparison of switch statements and ElseIf statements
In a switch statement, the condition is only one time, and then compared to each case, and in the ElseIf statement, the condition is evaluated again. If your condition is more complex, or multiple loops, using a switch statement will be faster

http://www.bkjia.com/PHPjc/629108.html www.bkjia.com true http://www.bkjia.com/PHPjc/629108.html techarticle This article describes in detail the use of the switch statement in PHP, as well as the switch case in the condition of 0 o'clock processing method, there is a need to understand the students can refer to. Switch statement If you want to ...

  • 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.