Can I use a hash sign (#) for commenting in PHP?

前端 未结 11 548
你的背包
你的背包 2021-01-31 13:07

I have never, ever, seen a PHP file using hashes (#) for commenting. But today I realized that I actually can! I\'m assuming there\'s a reason why everybody uses

相关标签:
11条回答
  • 2021-01-31 13:34

    One might think that the # form of commenting is primarily intended to make a shell script using the familiar "shebang" (#!) notation. In the following script, PHP should ignore the first line because it is also a comment. Example:

    #!/usr/bin/php
    <?php
    
    echo "Hello PHP\n";
    

    If you store it in an executable file you can then run it from a terminal like this

    ./hello
    

    The output is

    Hello PHP
    

    However, this reasoning is incorrect, as the following counterexample shows:

    #!/usr/bin/php
    #A
    <?php
    
    #B
    echo "Hello PHP\n";
    

    The first line (the shebang line) is specially ignored by the interpreter. The comment line before the PHP tag is echoed to standard output because it is not inside a PHP tag. The comment after the opening PHP tag is interpreted as PHP code but it is ignored because it is a comment.

    The output of the revised version is

    #A
    Hello PHP
    
    0 讨论(0)
  • 2021-01-31 13:35

    From https://php.net/manual/en/migration53.deprecated.php

    "Deprecated features in PHP 5.3.x ...Comments starting with '#' are now deprecated in .INI files."

    There you have it. Hash '#' appears to remain as a comment option by default by not being deprecated. I plan to use it to distinguish various layers of nested if/else statements and mark their closing brackets, or use to distinguish code comments from commented out code as others have suggested in related posts. (Note: Link was valid/working as of 4/23/19, although who knows if it'll still be working when you're reading this.)

    0 讨论(0)
  • 2021-01-31 13:37

    Yes, however there are cross platform differences.

    I use # all the time for commenting in PHP, but I have noticed an adoption difference.

    On windows keyboard the # key is easy to use. On mac keyboard # key mostly isn't present.

    So for mac users, [Alt] + [3] or [⌥] + [3] is more difficult to type than //, so // has become a cross platform way of displaying code with comments.

    This is my observation.

    0 讨论(0)
  • 2021-01-31 13:40

    Is there any reason, aside from personal preference, to use // rather than # for comments?

    I think it is just a personal preference only. There is no difference between // and #. I personally use # for one-line comment, // for commenting out code and /** */ for block comment.

    <?php
        # This is a one-line comment
        echo 'This is a test';
    
        // echo 'This is yet another test'; // commenting code
    
        /** 
         * This is a block comment
         * with multi-lines 
         */
        echo 'One final test';
    ?>
    
    0 讨论(0)
  • 2021-01-31 13:45

    PHP's documentation describes the different possibilities of comments. See http://www.php.net/manual/en/language.basic-syntax.comments.php

    But it does not say anything about differences between "//" and "#". So there should not be a technical difference. PHP uses C syntax, so I think that is the reason why most of the programmers are using the C-style comments '//'.

    0 讨论(0)
提交回复
热议问题