Introduction to switch statements in php _ PHP Tutorial

Source: Internet
Author: User
Php switch statement usage introduction. This article introduces the switch statement usage in php in detail and the solution for switchcase when the condition is 0. if you need to know it, refer to it. If you want to introduce the usage of the Switch statement in php in detail in this article, and how to handle the switch case when the condition is 0, you can refer to it for details.

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

The code is as follows:
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.

The code is as follows:

Switch ($ cps_sign ){
Case 'iqifa ':
Case 'chengguo ':
Case 'roiyiqifa ':
Case 'lkt ':
Case 'fanlil ':
Case 'qqfanlil ':
Case 'weiyi ':
Case 'yoyi ':
$ SQL = "INSERT into sa_cps_list ('uv','s _ time', 'CP', 'URL') VALUES ('{$ uv}', {$ timestamp }, '{$ cps_sign}', '{$ url }')";
Echo $ SQL; exit ();
Mysql_query ($ SQL );
Break;
Default:
Break;
}


After carefully reading the program, is it a fault caused by switch and case? Therefore, write the DEMO detection.
// The output result is: bool (true) bool (false) xxx.

The code is as follows:

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

$ Errid = '';
Switch ($ errid ){
Case 0:
Echo "xxx ";
Break;
Default:
Echo "yyy ";
}

In the original switch/case structure, the case value is = rather than =. In this way, null is equal to 0, and the result I receive is of course incorrect.
No way, the program does not want to change. after all, switch/case is more comfortable than if. Find a solution. Haha.

The code is as follows:

$ Result = '';
If (is_numeric ($ err_id) === false ){
$ Result. = 'downtime or timeout, no return value ';
Return $ result;
}
Switch ($ err_id ){
Case xxx:
..........
}

After the problem is solved, first determine whether the returned value is a numerical value. if it is not a numerical value, return directly.
Be careful when writing switch/case to Judge numbers in the future, especially when the loss value of 0 exists.

Comparison between switch statements and elseif statements
In the switch statement, the condition is evaluated only once and then compared with each case. in the elseif statement, the condition is evaluated again. If your conditions are complex or repeated loops, the switch statement will be faster.

When the limit case condition is 0, you can refer to it for more information. Switch statement if you want...

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.