Home  >  Article  >  Backend Development  >  Introduction to the usage of switch statement in php_PHP tutorial

Introduction to the usage of switch statement in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:491024browse

This article introduces in detail the usage of switch statement in PHP and how to handle switch case when the condition is 0. Students who need to know more can refer to it.

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

Use Switch statements to avoid lengthy if..elseif..else blocks.

Grammar

The code is as follows Copy code
switch (expression)
 代码如下 复制代码
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;
}
{

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;

}
 代码如下 复制代码

    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;
    }


Example

How it works:
 代码如下 复制代码

var_dump(""==0);   
var_dump(""===0);

$errid = '';
switch ($errid) {
    case 0 :
        echo "xxx";
        break;
    default:
        echo "yyy";
}

Perform an evaluation of an expression (usually a variable) Compare the value of the expression with the value of the case in the structure If there is a match, execute the code associated with case After the code is executed, the break statement prevents the code from jumping to 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; }
After taking a closer look at the program, could it be that switch and case were to blame? So, write DEMO test. //The output result 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 is revealed. It turns out that in the switch/case structure, == is used instead of === when comparing case values. In this way, empty is equal to 0, and the result I receive is of course wrong.
There is no way, I don’t want to change the program. After all, when there are multiple conditions, switch/case is more comfortable to watch than a bunch of ifs. Find a way. hehe.

} switch ($err_id) {
The code is as follows
 代码如下 复制代码

$result = '';
if(is_numeric($err_id) === false) { 
$result .= '宕机或超时,没有返回值';
return $result;
}
switch ($err_id) {
    case xxx :
        ..........
}

Copy code


$result = '';

if(is_numeric($err_id) === false) {
$result .= 'Downtime or timeout, no return value';

return $result;
case xxx :       …........

}

The problem is solved. First check whether the return value is a numerical value. If it is not a numerical value, return directly. You must be careful when writing switch/case judgment numbers in the future, especially when there is a prodigal value of 0 Comparison of switch statement and elseif statement In a switch statement, the condition is evaluated once and then compared to each case, whereas in an elseif statement, the condition is evaluated again. If your conditions are more complex, or there are multiple loops, it will be faster to use the switch statement
http://www.bkjia.com/PHPjc/629108.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629108.htmlTechArticleThis article introduces in detail the usage of switch statement in php, and how to handle switch case when the condition is 0 , students who need to know more can refer to it. Switch statement If you wish...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn