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

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

    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
    

    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
    

提交回复
热议问题