php中的if判断没有花括号,PHP - if / else,for,foreach,while - 没有花括号? (PHP - If/else, for, foreach, while - w...

As have said you don't wanna learn about shorthand's and accepted answer gives good example about omitting curly braces, but there is something to add. As you can see it's fine to omit curly braces in case of if ($x) echo 'foo';. There is nothing wrong with code, no performance or other issues and it is readable by other developers. And example also shows you that if you write

if ($x)

echo 'foo';

echo 'bar';

instead of

if ($x)

echo 'foo';

echo 'bar';

You can run into unwanted results where bar is printed while you don't want it to be printed and if your code is full of such statements then it will make it harder for you to read your own code and even more harder for others to read it.

I don't wanna learn about shorthand's I just want to understand the conditions about when and where it is possible to omit the curly brackets.

These things are closely related so if you really want to understand where it is possible to omit curly brackets then that should be a must that you understand or are at least aware of shorthand's , have read

So my big question is: When can I omit the curly braces and in which structure/loop/function?

The curly brace is not required however, for readability and maintenance, many developers would consider it bad style not to include them. Previous 2 links should give you information needed to make your own decisions when you could omit curly brace.

for example there is nothing wrong with following code snippets which all do exactly same thing.

With curly brace

if (PHP_VERSION_ID < 70000)

{

print "PHP >= 7.0 required yours is ";

print phpversion();

print "\n";

exit(1);

}

Is same as

if (PHP_VERSION_ID < 70000) :

print "PHP >= 7.0 required yours is ";

print phpversion();

print "\n";

exit(1);

endif;

Or you can use the dot operator

if (PHP_VERSION_ID < 80000)

(print "PHP >= 7.0 required yours is ") . (print phpversion()) . (print "\n") . exit(1);

And you can make use of the ternary conditional operator and even omit if it self besides omitting curly braces

(PHP_VERSION_ID > 70000) ?: (print "PHP >= 7.0 required yours is ") . (print phpversion()) . (print "\n") . exit(1);

Since we only print we can shorten that and strip some print string functions which were here to represent more than one function in statement without curly braces

(PHP_VERSION_ID > 70000) ?: (print "PHP >= 7.0 required yours is " . phpversion() . "\n") . exit(1);

As from php 7 we can use Null coalescing operator

(PHP_VERSION_ID > 70000) ?: null ?? (print "PHP >= 7.0 required yours is ".phpversion() . "\n") . exit(1);

As one can see that there is many ways you can get exactly the same result. That not only applies for this if example but same can be practiced with structure/loop/function. So there is no one answer for your big question. One should consider mainly following.

Is the code you are writing easy to maintain.

Can you answer for your self is there something you win by omitting curly braces.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值