Introduction to switch statements in php

Source: Internet
Author: User
Tags switch case
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 selectively execute one of several code blocks, use the Switch statement.

This article details the usage of the switch statement in php and the solution of the switch case when the condition is 0. if you need to know it, refer to it.

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.

The instance code is as follows:

  1. Switch (expression)
  2. {
  3. Case label1:
  4. Code to be executed if expression = label1;
  5. Break;
  6. Case label2:
  7. Code to be executed if expression = label2;
  8. Break;
  9. Default:
  10. Code to be executed
  11. If expression is different
  12. From both label1 and label2;
  13. }

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

  1. Switch ($ cps_sign ){
  2. Case 'iqifa ':
  3. Case 'chengguo ':
  4. Case 'roiyiqifa ':
  5. Case 'lkt ':
  6. Case 'fanlil ':
  7. Case 'qqfanlil ':
  8. Case 'weiyi ':
  9. Case 'yoyi ':
  10. $ SQL = "INSERT into sa_cps_list ('uv','s _ time', 'CP', 'URL') VALUES ('{$ uv}', {$ timestamp }, '{$ cps_sign}', '{$ url }')";
  11. Echo $ SQL; exit ();
  12. Mysql_query ($ SQL );
  13. Break;
  14. Default:
  15. Break;
  16. }

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

  1. Var_dump ("" = 0 );
  2. Var_dump ("" = 0 );
  3. $ Errid = '';
  4. Switch ($ errid ){
  5. Case 0:
  6. Echo "xxx ";
  7. Break;
  8. Default:
  9. Echo "yyy ";
  10. }

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, when there are multiple conditions, switch/case is much more comfortable than a bunch of if. find a solution. haha.

The instance code is as follows:

  1. $ Result = '';
  2. If (is_numeric ($ err_id) === false ){
  3. $ Result. = 'downtime or timeout, no return value ';
  4. Return $ result;
  5. }
  6. Switch ($ err_id ){
  7. Case xxx:
  8. ..........
  9. }

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.

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.