Unexplained syntax error with logical OR ( || )

后端 未结 1 878
野性不改
野性不改 2021-02-14 22:35

I\'m working with NetBeans for Mac, and I\'m running CakePHP (though I don\'t think the framework has anything to do with it) in a shared hosting in Linux. This is not a big iss

相关标签:
1条回答
  • 2021-02-14 22:47

    I'm working With NetBeans for MAC

    When is a space not a space?

    When it's a non-breaking space!

    The intention is:

    " || "
    207C7C20 (hex)
    

    But what is actually in the source file is almost certainly:

    " || "
    207C7CA0 (hex)
    

    (on stack overflow it won't be but I bet it is in the source file).

    With a mac the problem is (using my own keyboard layout, but I am assuming it's similar in your case):

    "|" = alt + 1
    " " = alt + space (accidental)
    

    So typing away, with the sequence " || " it's very easy for the alt key to still be depressed when the space bar is pressed and: voilà you get unexpected "wat" syntax errors which at face value make no sense - until you realize what the problem is.

    Example:

    -> cat foo.php 
    <?php
    
    $foo = "x";
    if (true || $foo) {
    }
    -> php -l foo.php 
    
    Parse error: syntax error, unexpected '$foo' (T_VARIABLE) in foo.php on line 4
    
    Errors parsing foo.php
    
    0 讨论(0)
提交回复
热议问题