PHP - 从 IF 块退出

发布于 2024-10-09 07:02:16 字数 212 浏览 4 评论 0原文

如果满足特定条件,如何退出 if 块?

我尝试使用 break 但它不起作用:

if($bla): 
  $bla = get_bla();
  if(empty($bla)) break;
  do($bla);
endif;

它说:致命错误:无法中断/继续 1 级...

How can I exit a if block if a certain condition is met?

I tried using break but it doesn't work:

if($bla): 
  $bla = get_bla();
  if(empty($bla)) break;
  do($bla);
endif;

it says: Fatal error: Cannot break/continue 1 level in...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

浅沫记忆 2024-10-16 07:02:16

在 PHP 5.3 中,您可以使用 goto

if($bla): 
   $bla = get_bla();
   if(empty($bla)) goto end;
   do($bla);
endif;
end:

但我个人认为这是一个丑陋的解决方案。

In PHP 5.3 you can use goto

if($bla): 
   $bla = get_bla();
   if(empty($bla)) goto end;
   do($bla);
endif;
end:

But personally I think that's an ugly solution.

肤浅与狂妄 2024-10-16 07:02:16

您不能中断 if 语句,只能中断 for 或 while 等循环。

如果此 if 在函数中,请使用“return”。

You can't break if statements, only loops like for or while.

If this if is in a function, use 'return'.

同展鸳鸯锦 2024-10-16 07:02:16

为什么不把它扭转过来呢?

if($bla): 
  $bla = get_bla();
  if(!empty($bla)) {
    do($bla);
  }
endif;

这样它只会在 $bla 不为空时运行你的代码..这就是 if 语句的要点

Why not just turn it around.

if($bla): 
  $bla = get_bla();
  if(!empty($bla)) {
    do($bla);
  }
endif;

That way it will only run your code if $bla isn't empty.. That's kinda the point with if-statements

黑寡妇 2024-10-16 07:02:16

我不敢相信还没有人发布这个解决方案(用我的 PHP 风格编写):

if($bla){do{
  $bla = get_bla();
  if(empty($bla)) break;
  do($bla);
}while(false);}

复杂性仍然是 O(1)

I cant believe no one have post this solution yet (writing it in my PHP style):

if($bla){do{
  $bla = get_bla();
  if(empty($bla)) break;
  do($bla);
}while(false);}

Complexity still O(1)

_蜘蛛 2024-10-16 07:02:16

试试这个

do {

    if($bla) {
        $bla = get_bla();

        if (empty($bla)) {
            break;
        }

        do($bla);
    }

    /* You can do more comparisions here */

} while (0);

Try this

do {

    if($bla) {
        $bla = get_bla();

        if (empty($bla)) {
            break;
        }

        do($bla);
    }

    /* You can do more comparisions here */

} while (0);
私藏温柔 2024-10-16 07:02:16

你不能。

您可以将整个事情放入一个可以返回的函数中。

否则,您将必须更改逻辑并包含另一个 if 块。

if($bla): 
  $bla = get_bla();
  if(!empty($bla)): 
   do($bla);
  endif;
endif;

You can't.

You could put the whole thing into a function from which you can return.

Otherwise, you'll have to change the logic and include another if block.

if($bla): 
  $bla = get_bla();
  if(!empty($bla)): 
   do($bla);
  endif;
endif;
吲‖鸣 2024-10-16 07:02:16

对我来说,如果您不介意在另一个块中使用 if 语句,那么拥有一个转义标记会很有帮助,以防代码需要在块之间退出。

$exit = FALSE;

if(!$exit){
    if($data["param1"] == cond1){
        //do something and continue
    }
    else{
        //do something
        $exit = TRUE;
    }
}

if(!$exit){
    if($data["param2"] == cond2){
        //do something and continue
    }
    else{
        //do something
        $exit = TRUE;
    }
}

{...}

如果您继续在每个块周围放置条件语句,则在将 $exit 设置为 true 后,它将不会执行任何其他块。您可以将变量命名为 $continue 并恢复其角色(如果这对您来说更有意义)。

如果你没有 else 语句,它会更容易工作。

$exit = FALSE;

if($bla): 
    $bla = get_bla();
    if(empty($bla)) $exit = TRUE;
    if(!$exit)do($bla);
endif;

For me it helps to have an escape marker in case the code needs to exit between blocks if you don't mind having an if statement in another.

$exit = FALSE;

if(!$exit){
    if($data["param1"] == cond1){
        //do something and continue
    }
    else{
        //do something
        $exit = TRUE;
    }
}

if(!$exit){
    if($data["param2"] == cond2){
        //do something and continue
    }
    else{
        //do something
        $exit = TRUE;
    }
}

{...}

If you keep placing conditional statements around each block, it will not execute any other blocks after you set $exit to true. You can name the variable $continue and revert its roles if that makes more sense to you.

it works easier if you have no else statements.

$exit = FALSE;

if($bla): 
    $bla = get_bla();
    if(empty($bla)) $exit = TRUE;
    if(!$exit)do($bla);
endif;
长不大的小祸害 2024-10-16 07:02:16

严格来说,我们不能过早地离开 if 块,但有时确实需要这样做。 break 只能在循环和 switch 块内使用。但我们可以将 if 放在循环中。所以,马科斯·费尔南德斯·拉莫斯的回答可能是最合适的。这是稍微好一点的变体。

do if ()
{
  ...
  if () break;
  ...
} while (false);

Strictly speaking, we can't leave if-block prematurely, but sometimes it's really needed. break can be used only inside loops and switch-block. But we can place if in a loop. So, answer of Marcos Fernandez Ramos is probably the most appropriate. Here is just slightly nicer variant.

do if ()
{
  ...
  if () break;
  ...
} while (false);
北城孤痞 2024-10-16 07:02:16

如果您确实想要一个可以break的语句,则可以使用while语句:

while ($bla) {
    $bla = get_bla();

    if (!$bla) {
        break;
    }

    do($bla);

    // Maybe some more code...

    // Do not forget to break out of the while loop!
    break;
}

这是避免许多嵌套if的好方法语句(在许多情况下可以避免),但要小心打破循环。

If you really want a statement from which you can break, you could use a while statement:

while ($bla) {
    $bla = get_bla();

    if (!$bla) {
        break;
    }

    do($bla);

    // Maybe some more code...

    // Do not forget to break out of the while loop!
    break;
}

This is a good way of avoiding many nested if statements (which can maybe be avoided in many cases), but do be careful to break the loop.

早乙女 2024-10-16 07:02:16

如果您的条件是针对单个变量的测试,那么 switch 语句是一个更好的解决方案,您可以轻松突破:

switch ($var_to_check) { // equivalent to if ($var_to_check==$value_against)
    case $value_against:
        ...
        if ($something_wrong) { break; } // early exit from if
        ...
        break; // regular break
    default: // equivalent to else
        ...
} // end of switch (or if)
... // after the tests

它仅适用于单个测试检查。具有处理一堆 else if 的优点。

if your condition is a test against a single variable, then the switch statement is a much better solution and you can easily break out:

switch ($var_to_check) { // equivalent to if ($var_to_check==$value_against)
    case $value_against:
        ...
        if ($something_wrong) { break; } // early exit from if
        ...
        break; // regular break
    default: // equivalent to else
        ...
} // end of switch (or if)
... // after the tests

It only works for single test checks. Has the advantage of handling a bunch of else ifs.

沫雨熙 2024-10-16 07:02:16

只需将 IF 替换为 WHILE 并在新 while 语句的末尾以及中间的任意位置添加 break; 即可。

Just replace IF with WHILE and add break; at the end of the new while statement and anywhere in middle too.

铁轨上的流浪者 2024-10-16 07:02:16

您可以为 IF 块的包含内容创建一个函数。
例如

if($bla): 
  doSomething();
endif;

然后你可以简单地使用 RETURN

function doSomething() {
    $bla = get_bla();
    if(empty($bla)) return;
    do($bla);
}

它看起来比乘以 IF 语句更清楚

You can create a function for the contains of your IF block.
For example

if($bla): 
  doSomething();
endif;

Then you can simply use RETURN

function doSomething() {
    $bla = get_bla();
    if(empty($bla)) return;
    do($bla);
}

It's looking more clearly than multiply IF statements

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文