PHP - 从 IF 块退出
如果满足特定条件,如何退出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在 PHP 5.3 中,您可以使用 goto
但我个人认为这是一个丑陋的解决方案。
In PHP 5.3 you can use goto
But personally I think that's an ugly solution.
您不能中断 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'.
为什么不把它扭转过来呢?
这样它只会在 $bla 不为空时运行你的代码..这就是 if 语句的要点
Why not just turn it around.
That way it will only run your code if $bla isn't empty.. That's kinda the point with if-statements
我不敢相信还没有人发布这个解决方案(用我的 PHP 风格编写):
复杂性仍然是 O(1)
I cant believe no one have post this solution yet (writing it in my PHP style):
Complexity still O(1)
试试这个
Try this
你不能。
您可以将整个事情放入一个可以
返回
的函数中。否则,您将必须更改逻辑并包含另一个 if 块。
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 语句,那么拥有一个转义标记会很有帮助,以防代码需要在块之间退出。
如果您继续在每个块周围放置条件语句,则在将 $exit 设置为 true 后,它将不会执行任何其他块。您可以将变量命名为 $continue 并恢复其角色(如果这对您来说更有意义)。
如果你没有 else 语句,它会更容易工作。
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 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.
严格来说,我们不能过早地离开
if
块,但有时确实需要这样做。break
只能在循环和switch
块内使用。但我们可以将if
放在循环中。所以,马科斯·费尔南德斯·拉莫斯的回答可能是最合适的。这是稍微好一点的变体。Strictly speaking, we can't leave
if
-block prematurely, but sometimes it's really needed.break
can be used only inside loops andswitch
-block. But we can placeif
in a loop. So, answer of Marcos Fernandez Ramos is probably the most appropriate. Here is just slightly nicer variant.如果您确实想要一个可以
break
的语句,则可以使用while
语句:这是避免许多嵌套
if
的好方法语句(在许多情况下可以避免),但要小心打破
循环。If you really want a statement from which you can
break
, you could use awhile
statement:This is a good way of avoiding many nested
if
statements (which can maybe be avoided in many cases), but do be careful tobreak
the loop.如果您的条件是针对单个变量的测试,那么 switch 语句是一个更好的解决方案,您可以轻松突破:
它仅适用于单个测试检查。具有处理一堆 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:
It only works for single test checks. Has the advantage of handling a bunch of else ifs.
只需将 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.您可以为 IF 块的包含内容创建一个函数。
例如
然后你可以简单地使用 RETURN
它看起来比乘以 IF 语句更清楚
You can create a function for the contains of your IF block.
For example
Then you can simply use RETURN
It's looking more clearly than multiply IF statements