2013-08-06 29 views
1

我将尝试用注释代码解释我想实现的内容。如果条件符合,则跳过if语句并继续执行下面的代码php

我想要做的是跳过if语句,如果条件满足并继续执行条件语句之外的代码。

<?php 
    if (i>4) { 
    //if this condition met skip other if statements and move on 
    } 

    if (i>7) { 
    //skip this 
?> 

<?php 
    move here and execute the code 
?> 

我知道break,continue,end和return语句,但这不适用于我的情况。

我希望这可以清除我的问题。

+0

中断并继续工作只在循环内。 –

+1

你可以简单地包装什么应该跳过一个else语句?这是我认为如果 - 其他 - 是有原因的。正如有人早些时候所说的那样:if(i> 4)和if(i> 7)如何相关。如果他们应该使用if(i> 7)应该是第一个检查条件。 – Alex

回答

1

我通常设置某种标记,如:

<?php 
    if (i>4) 
    { 
    //if this condition met skip other if statements and move on 
    $skip=1; 
    } 

    if (i>7 && !$skip) 
    { 
    //skip this 
    ?> 

    <?php 
    move here and execute the code 
    ?> 
4

如果你的第一个条件满足,你想跳过其他条件,你可以使用任何标志变量如下:

<?php 
     $flag=0; 
     if (i>4) 
     { 
      $flag=1; 
     //if this condition met skip other if statements and move on 
     } 

     if (i>7 && flag==0) 
     { 
     //skip this 
     ?> 

     <?php 
     move here and execute the code 
     ?> 
+0

为什么不把其他的if语句放在第一个“else”中呢? –

2

您可以使用goto

<?php 
if (i>4) 
{ 
//if this condition met skip other if statements and move on 
goto bottom; 
} 

if (i>7) 
{ 
//skip this 
?> 

<?php 
bottom: 
// move here and execute the code 
// } 
?> 

但是再一次,寻找恐龙。

goto xkcd

3

使用if-elseif-else

if($i > 4) { 
    // If this condition is met, this code will be executed, 
    // but any other else/elseif blocks will not. 
} elseif($i > 7) { 
    // If the first condition is true, this one will be skipped. 
    // If the first condition is false but this one is true, 
    // then this code will be executed. 
} else { 
    // This will be executed if none of the conditions are true. 
} 

结构上,这应该是你在找什么。尽量避免任何会导致意大利面代码的东西,如goto,breakcontinue

在附注中,您的条件没有多大意义。如果$i不大于4,它永远不会大于7,所以第二个块永远不会被执行。

0
<?php 
    while(true) 
    { 
    if (i>4) 
    { 
    //if this condition met skip other if statements and move on 
    break; 
    } 

    if (i>7) 
    { 
    //this will be skipped 
    } 
    }  
?> 

    <?php 
    move here and execute the code 
    ?> 
+0

如果你想避免无限循环期间所有false..use int i = 0; while(i <1){//你的if语句;我++; –

相关问题