IF嵌套如何优化写法?

WBOY
Release: 2016-06-23 13:24:42
Original
1368 people have browsed it

像这样的IF条件还能再怎么写好,应该不能用switch,毕竟要判断时间、次数、IP等条件。
if(访问时间段){
     echo "正常时间段";
     if(ID范围){
         echo "正常ID范围";
         if(访问次数){
             echo "正常访问次数";
             if(IP地址){
                 echo "正常IP地址";
                 }else{
                 echo "错误IP地址";
                 }
             }else{
             echo "访问次数过多";
             }
         }else{
         echo "错误ID范围";
         }
     }
else{
     echo "请注意休息";
     }


回复讨论(解决方案)

平行的写法

while(true) {  if(访问时间段){    echo "正常时间段";  }else {    echo "请注意休息";    break;  }  if(ID范围){    echo "正常ID范围";  }else {    echo "错误ID范围";    break;  }  if(访问次数){    echo "正常访问次数";  }else {    echo "访问次数过多";    break;  }  if(IP地址){    echo "正常IP地址";  }else{    echo "错误IP地址";    break;  }  break;}
Copy after login

$error = 0;if(访问时间段){     echo "正常时间段";else{    $error = 1;    echo "请注意休息";}if($error==0 && ID范围){    echo "正常ID范围";}else{    $error = 1;    echo "错误ID范围";}if($error==0 && 访问次数){    echo "正常访问次数";}else{    $error = 1;    echo "访问次数过多";}if($error==0 && IP地址){    echo "正常IP地址";}else{    $error = 1;    echo "错误IP地址";}
Copy after login

二楼的平行结构很好理解,三楼的没用过哦……

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template