PHP conditionals, brackets needed?

前端 未结 7 2257
深忆病人
深忆病人 2020-11-28 05:55

I was just browsing a forum and someone asked about a PHP file they had found on the web. It has several spots like this in the code:

if ($REMOTE_ADDR == \"\"

7条回答
  •  臣服心动
    2020-11-28 06:51

    Yes, excluding the braces is allowed, although many times I have heard 2 reasons for not using that syntax:

    1. It's harder to read. Less obvious to another programmer.
    2. If you ever wany to add something inside the if, then you need to add the braces which is harder after then when you're first coding since most editors will add the closing brace for you.

    Also, yes, the colon syntax is valid. The alternatives can be found here: http://php.net/manual/en/control-structures.alternative-syntax.php

提交回复
热议问题