PHP - If/else, for, foreach, while - 没有花括号? [英] PHP - If/else, for, foreach, while - without curly braces?

查看:38
本文介绍了PHP - If/else, for, foreach, while - 没有花括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真正想知道但从未发现的东西是 PHP 中的快捷方式.

我目前正在编写一个带有 foreach 循环的函数,里面只有一个语句.我试图省略花括号,就像在 if/else 控制结构中一样,它可以工作.没有错误.

foreach($var as $value)$arr[] = $value;

现在我尝试以相同的方式使用它,但在其中放置了一个 if/else 块.再次运行,没有错误.

foreach($var as $value)如果(1 + 1 == 2){$arr[] = $value;};

然后,我想为什么这行得通?"并省略了结束分号.还在工作.因此,我尝试在 foreach 循环中再次使用不带花括号的 if/else 语句,仍然有效且没有错误.但是 foreach 循环现在真的关闭/结束了吗?

foreach($var as $value)如果(1 + 1 == 2)$arr[] = $value;

至少我再次省略了结束分号并且(正如预期的那样)发生了解析错误.

所以我的大问题是:我什么时候可以省略花括号以及在哪个结构/循环/函数中?我知道我绝对可以在 ifelse 中这样做.但是whileforforeach 呢?

是的,我知道它不安全,不聪明,无论什么都不用花括号编码并且有像 $condition 这样的速记?true : false;if: doSomething();endif;endfor;endforeach;.我不想学习速记,我只想了解有关何时何地可以省略大括号的条件.

解决方案

当您省略大括号时,它只会将下一条语句视为条件体.

if ($x) echo 'foo';

相同

if ($x) { echo 'foo';}

但请记住

if ($x)回声'富';回声'酒吧';

总是打印bar"

在内部,情况正好相反:if 只会查看下一个表达式,但 PHP 将 {} 中的所有内容视为单个分组"表达式.>

其他控制语句相同(foreach,等等)

Something that really would like to know but never found out are shortcuts in PHP.

I am currently coding a function with a foreach loop with just a single statement inside. I tried to omit the curly braces as you can do in if/else control structures and it works. No errors.

foreach($var as $value)
    $arr[] = $value;

Now I tried to use it the same way but putting an if/else block inside it. Again, working and no errors.

foreach($var as $value)
    if(1 + 1 == 2) {
        $arr[] = $value;
    };

Then, I thought like "why is this working?" and omitted the closing semicolon. Still working. So I tried to use the if/else statement without curly braces inside the foreach loop and again, still working and no errors. But is the foreach loop really closed/ended right now?

foreach($var as $value)
    if(1 + 1 == 2)
        $arr[] = $value;

At least I omitted the closing semicolon again and (as expected) a parsing error occurred.

So my big question is: When can I omit the curly braces and in which structure/loop/function? I know that I can definitely do so in if and else. But what about while, for and foreach?

And yes, I know that it is not safe, smart, whatever to code without curly braces and there are shorthands like $condition ? true : false; and if: doSomething(); endif;, endfor; and endforeach;. I don't wanna learn about shorthands I just want to understand the conditions about when and where it is possible to omit the curly brackets.

解决方案

When you omit the braces it will only treat the next statement as body of the condition.

if ($x) echo 'foo';

is the same as

if ($x) { echo 'foo'; }

but remember that

if ($x)
  echo 'foo';
  echo 'bar';

will always print "bar"

Internally it's the other way around: if will only look at the next expression, but PHP treats everything in {} as a single "grouped" expression.

Same for the other control statements (foreach, and so on)

这篇关于PHP - If/else, for, foreach, while - 没有花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆