Home > Backend Development > PHP Tutorial > IF嵌套怎么优化写法

IF嵌套怎么优化写法

WBOY
Release: 2016-06-13 12:25:52
Original
1691 people have browsed it

IF嵌套如何优化写法?
像这样的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) {<br />  if(访问时间段){<br />    echo "正常时间段";<br />  }else {<br />    echo "请注意休息";<br />    break;<br />  }<br />  if(ID范围){<br />    echo "正常ID范围";<br />  }else {<br />    echo "错误ID范围";<br />    break;<br />  }<br />  if(访问次数){<br />    echo "正常访问次数";<br />  }else {<br />    echo "访问次数过多";<br />    break;<br />  }<br />  if(IP地址){<br />    echo "正常IP地址";<br />  }else{<br />    echo "错误IP地址";<br />    break;<br />  }<br />  break;<br />}
Copy after login

------解决思路----------------------
<br />$error = 0;<br />if(访问时间段){<br />     echo "正常时间段";<br />else{<br />    $error = 1;<br />    echo "请注意休息";<br />}<br /><br />if($error==0 && ID范围){<br />    echo "正常ID范围";<br />}else{<br />    $error = 1;<br />    echo "错误ID范围";<br />}<br /><br />if($error==0 && 访问次数){<br />    echo "正常访问次数";<br />}else{<br />    $error = 1;<br />    echo "访问次数过多";<br />}<br /><br />if($error==0 && IP地址){<br />    echo "正常IP地址";<br />}else{<br />    $error = 1;<br />    echo "错误IP地址";<br />}<br />
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