Semicolon after if condition in PHP - code still works

前端 未结 2 1861
借酒劲吻你
借酒劲吻你 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;
    
    0 讨论(0)
  • 2021-01-21 11:44

    becuse you can write any code inside { } without if

    check this example:

    <?php
    
    {
        echo 'Hi Man'; // it print Hi Man (without using if statment)
    }
    
    ?>
    
    0 讨论(0)
提交回复
热议问题