【发布时间】:2014-09-01 09:13:59
【问题描述】:
我正在处理我继承的遗留代码并且我遇到了这个代码。
- 此代码有效!
- 忽略变量名看
{第一个Condition打开但从不关闭else有没有打开?还能用吗? - 它是在两种情况下继续还是仅在一种情况下继续,如果是哪一种情况?
-
为什么它甚至可以工作?
foreach($bla as $foo) { if(condition) { if(condition) { // Do Something }else continue; } }
我的假设是内部条件将使用 else?但我只是假设因为这个答案:Nested if-else behaviour without braces我找不到 PHP 的明确答案
【问题讨论】:
-
Ignore the variable names look at the `{` the first Condition opens but never closes the else has no open? it still works?哦,但它确实关闭了(倒数第二个关闭})。 -
这里的关键是a single statement doesn't have to be wrapped in braces。您的
continue;行等同于{ continue; }。我想你剩下的困惑只是缩进。我个人建议在单个语句周围使用大括号,但这可能仍被视为宗教问题。
标签: php