Semicolon after if condition in PHP - code still works

前端 未结 2 1863
借酒劲吻你
借酒劲吻你 2021-01-21 11:27

consider the following code...

if ($condition); // <-- do not miss semicolon here!
{
    //...
}

After that code inside block works. Can som

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 11:37

    I would recommend you to read the manual here:

    http://php.net/manual/en/control-structures.if.php

    To you question directly why: you don't get a syntax error?
    ->Simple because there is no syntax error!

    Your code is correct and means this:

    if ($condition) ;
    //  ^condition  ^if true execute that line
    
    //same as
    if ($condition)
        ;
    
    //same example with other line if the condition is true
    if ($condition) echo "true";
    
    if ($condition)
        echo "true";
    

    So your line which gets executed if the condition is true is this: ; and means nothing.

    it's the same as: ;;;;; It's just nothing!

    In most of the time you use a if statement like this:

    if ($condition)
        echo $result;
    
    
    if ($condition) {
        echo $result;
    }
    
    if ($condition) echo $result;
    

提交回复
热议问题