PHP conditionals, brackets needed?

前端 未结 7 2187
深忆病人
深忆病人 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条回答
  •  -上瘾入骨i
    2020-11-28 06:42

    To go into a little more detail, the reason that the braces are optional is that the syntax looks like:

    if(CONDITION) BLOCK
    [elseif(CONDITION) BLOCK]
    [else BLOCK]
    

    BLOCK can be a single statement:

    foo();
    

    or it can be a brace-enclosed group of statements:

    {
        foo();
        bar();
    }
    

提交回复
热议问题