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

前端 未结 11 546
你的背包
你的背包 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:23

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

    I came here for the answer myself, and its good to know there is NO code difference.

    However, preference-wise one could argue that you'd prefer the 'shell->perl->php' comment consistency vs the 'c->php' way.

    Since I did approach php as a poor man's webby perl, I was using #.. and then I saw someone else's code and came straight to SO. ;)

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

    2021 UPDATE: As of PHP 8, the two characters are not the same. The sequence #[ is used for Attributes.(Thanks to i336 for the comment)

    Original Answer:

    The answer to the question Is there any difference between using "#" and "//" for single-line comments in PHP? is no.

    There is no difference. By looking at the parsing part of PHP source code, both "#" and "//" are handled by the same code and therefore have the exact same behavior.

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

    There's no official PSR for that.

    However, in all PSR example code, they use // for inline comments.

    There's an PSR-2 extension proposal that aims to standardize it, but it's not official: https://github.com/php-fig-rectified/fig-rectified-standards/blob/master/PSR-2-R-coding-style-guide-additions.md#commenting-code

    // is more commonly used in the PHP culture, but it's fine to use # too. I personally like it, for being shorter and saving bytes. It's personal taste and biased, there's no right answer for it, until, of course, it becomes a standard, which is something we should try to follow as much as possible.

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

    If you establish some rule sets in your team / project... the 2 types of comments can be used to outline the purpose of the commented code.

    For example I like to use # to mute / disable config settings, sub functions and in general a piece of code that is useful or important, but is just currently disabled.

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

    Comments with "#" are deprecated with PHP 5.3. So always use // or /.../

    0 讨论(0)
  • 2021-01-31 13:34
    <?php
        echo 'This is a test'; // This is a one-line C++ style comment
        /* This is a multi-line comment.
           Yet another line of comment. */
        echo 'This is yet another test.';
        echo 'One Final Test'; # This is a one-line shell-style comment
    ?>
    

    RTM

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