php andif语句,通过

时间:2017-12-23 08:54:16

标签: php if-statement goto fall-through

冒着把自己归咎于骨头的风险,我仍然会问这样的问题: 是否有像#34; andif"在PHP或我怎么能以优雅的方式解决下面的问题?

场景:首先测试,如果是,则进行一些处理(例如联系服务器),然后进行第二次测试,做某事......进行第三次测试,然后进行结果或 - 如果上述任何一项失败 - - 始终输出相同的故障。

而不是每次都重复else语句......

if ( ....) { 
        contact server ...
        if (  ...  ){
        check ...       
            if (  ... )   {
                success  ;
            } else {  failure ...       }
        } else {  failure ...       }
} else {  failure ...       }

..我寻找类似的东西:

if ( ...) {
   do something...
   andif ( test ) {
      do something more ...
      andif ( test) {
         do }
else { 
   collective error }

在一个功能我可以使用'跌倒'如果成功则返回模拟:

function xx {
 if {... if {... if {...  success; return; }}}
 failure
}

..但是在主程序中?

3 个答案:

答案 0 :(得分:0)

PHP中没有tabLayout.getContext(); 运算符,但您可以使用早期返回(或“失败快速”)习惯用法,并在测试失败时返回失败。这样,您就不需要一堆andif s:

else

答案 1 :(得分:0)

我会先检查错误:

if (not_true) {
    return;
}

connect_server; 

if (second_not_true) {
    return;
}

check;

等等......

您还可以使用logical operators在一个if语句中进行多项检查。例如:

if (test && second_test && third_test) { // means if test is true and if second_test is true and if third_test is true
    // do the stuff if success...
} else {
    // do the stuff if errors...
}

答案 2 :(得分:0)

好吧,因为在php中没有像andif这样的东西,我认为唯一的办法就是 - 屏住呼吸: 去 (对不起破坏圣诞节......)

   if  ( ....) { 
            contact server ...
            if (  ...  ){
            check ...       
                if (  ... )   {
                    success  ;
                    goto success;
                 }}}
    failure;
    success:
    continue...

在我看来,其他一切都变得更加复杂。