PHP - 从 IF 块退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4545769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - exit from IF block
提问by Alex
How can I exit a if
block if a certain condition is met?
if
如果满足特定条件,我如何退出块?
I tried using break
but it doesn't work:
我尝试使用break
但它不起作用:
if($bla):
$bla = get_bla();
if(empty($bla)) break;
do($bla);
endif;
it says: Fatal error: Cannot break/continue 1 level in...
它说:致命错误:无法打破/继续 1 个级别...
采纳答案by Sondre
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
这样它只会在 $bla 不为空的情况下运行你的代码......这就是 if 语句的重点
回答by Burak Guzel
回答by Oli
You can't break if statements, only loops like for or while.
您不能中断 if 语句,只能使用 for 或 while 之类的循环。
If this if is in a function, use 'return'.
如果此 if 在函数中,请使用“return”。
回答by Greeso
Try this
尝试这个
do {
if($bla) {
$bla = get_bla();
if (empty($bla)) {
break;
}
do($bla);
}
/* You can do more comparisions here */
} while (0);
回答by Marcos Fernandez Ramos
I cant believe no one have post this solution yet (writing it in my PHP style):
我不敢相信还没有人发布此解决方案(以我的 PHP 风格编写):
if($bla){do{
$bla = get_bla();
if(empty($bla)) break;
do($bla);
}while(false);}
Complexity still O(1)
复杂度仍然是 O(1)
回答by user3709234
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.
对我来说,如果您不介意在另一个语句中使用 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;
}
}
{...}
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.
如果您一直在每个块周围放置条件语句,则在将 $exit 设置为 true 后,它将不会执行任何其他块。您可以命名变量 $continue 并恢复其角色,如果这对您更有意义。
it works easier if you have no else statements.
如果您没有 else 语句,它会更容易工作。
$exit = FALSE;
if($bla):
$bla = get_bla();
if(empty($bla)) $exit = TRUE;
if(!$exit)do($bla);
endif;
回答by Pekka
You can't.
你不能。
You could put the whole thing into a function from which you can return
.
您可以将整个内容放入一个函数中,您可以从中获取return
。
Otherwise, you'll have to change the logic and include another if block.
否则,您将不得不更改逻辑并包含另一个 if 块。
if($bla):
$bla = get_bla();
if(!empty($bla)):
do($bla);
endif;
endif;
回答by Pekka
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.
严格来说,我们不能if
过早离开-block,但有时确实需要它。break
只能在循环和switch
-block内使用。但是我们可以放在if
一个循环中。所以,马科斯·费尔南德斯·拉莫斯的回答可能是最合适的。这是稍微好一点的变体。
do if ()
{
...
if () break;
...
} while (false);
回答by Marian
If you really want a statement from which you can break
, you could use a while
statement:
如果你真的想要一个你可以从中得到的声明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;
}
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.
这是避免许多嵌套if
语句(在许多情况下可以避免)的好方法,但要小心break
循环。
回答by Jacques Amar
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 语句是一个更好的解决方案,您可以轻松突破:
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.
它仅适用于单个测试检查。具有处理一堆 else ifs 的优势。