162

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 // instead though, so here I am.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • 20
    That's a hash (or pound, or square, depending on which country you are in), not a hash tag. A hashtag is a means of categorising content on Twitter. – Quentin Feb 01 '12 at 09:43
  • You could use the HTML escape equivalent # if you need the # symbol in your code – dotoree Feb 01 '12 at 09:53
  • 25
    I thought the `#` symbol was called a hash tag... :( No reason to down vote so heavily. Lesson learnt – Hubro Feb 01 '12 at 10:35
  • 6
    I like to use `#` for single line comments, `//` for commenting out code & `/* ... */` for comment blocks – John Magnolia Nov 15 '13 at 18:56
  • Possible duplicate of [PHP Comments # vs //](http://stackoverflow.com/questions/1935565/php-comments-vs) – nawfal Oct 18 '15 at 01:51

12 Answers12

183

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.

Aziz
  • 20,065
  • 8
  • 63
  • 69
  • 3
    Note that N++ (6.55) can't always fold `#` comments correctly. I noticed that in large PHP files: 2k lines or more. Sometimes it starts to fold code on multiple #. – CoR Apr 14 '14 at 13:47
  • 1
    I much prefer `#` comments over `//` ones .. but I've always been wondering if `#` is PSR complient .. Is it ? – Stphane Aug 11 '15 at 07:36
  • 5
    Hash is helpful when describing routes, eg. `# /news (code here)` instead of `// /news (code here)`. As for 2k LoC files, I think there are other problems than which comment tag to use :) – Juha Untinen Apr 01 '16 at 06:48
  • 3
    **AS OF PHP 8 THIS IS NO LONGER THE CASE: `#` will always be a comment operator, but the sequence `#[` now marks the start of an "[attribute](https://stitcher.io/blog/attributes-in-php-8)" or annotation.** (Which are pretty cool.) It's a very small change (and can be worked around by adding a space, like `# [`), but means `#` can no longer be treated as "ignore everything to end of line." So maybe don't use `#` in autogenerated code. – i336_ Jan 01 '21 at 12:27
  • 1
    Thank you, @i336_. I've updated the answer to reflect this update. – Aziz Jan 01 '21 at 16:33
  • N++ = [Notepad++](https://en.wikipedia.org/wiki/Notepad%2B%2B) ([Windows](https://en.wikipedia.org/wiki/Microsoft_Windows) only) – Peter Mortensen Jul 03 '23 at 14:05
11

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 '//'.

stollr
  • 6,534
  • 4
  • 43
  • 59
  • 1
    Or it uses perl syntax, in which case "#" makes its appearance. And perl gets its comment syntax from the unix-ey shells. – Gerard ONeill Jun 17 '19 at 12:41
7

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';
?>
Sithu
  • 4,752
  • 9
  • 64
  • 110
  • I like to use `//` for regular code comments, since that's what most people use when commenting out code. And I use `#` for comments that are intended to describe, rather than be code that is commented out. Avoiding `/**/` for one liners reduces opening / closing conflicts when you try to use `/**/` on code that has `/**/ within that code... you end up with premature closing. and that's bad. – ahnbizcad May 06 '16 at 19:49
  • Re *"There is no difference between // and #"*: That is [no longer the case](https://stackoverflow.com/questions/9093609/can-i-use-a-hash-sign-for-commenting-in-php/9093787#9093787). – Peter Mortensen Jul 03 '23 at 14:07
7
<?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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ajreal
  • 46,720
  • 11
  • 89
  • 119
6

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
Brandin
  • 906
  • 11
  • 20
  • 13
    Actually, the shebang is **outside** the PHP code, so it is absolutely **not a comment for PHP**. Try removing the `!`, and run the file through `php` command line: it will print "#/usr/bin/php". The reason why the shebang is ignored is because PHP recognize shebang lines at the very begining of files and ignore them. – Ninj Feb 18 '15 at 17:24
  • Using php7.4, both comments are echoed. So the sheband is not (or no longer) ignored at all. – Chargnn Jan 15 '20 at 21:25
  • @Chargnn, just tested it, it works in PHP 7.4 as well. It shouldn't depend on the PHP version, this comment means nothing to PHP, only to the shell. Shell sees this comment, reads it, then removes it and passes the rest to PHP. Maybe you weren't running it in a unix shell or some weird shell that doesn't support this? – egst Feb 25 '21 at 13:47
1

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

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.

d.raev
  • 9,216
  • 8
  • 58
  • 79
0

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.

Community
  • 1
  • 1
Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
  • The problem with standards in the Computer Science realm is that to make a standard, you have to have the best option, and in Computer Science there is no such thing as the best option. There are only wrong options, and better options. But "best option" does not exist. –  Apr 12 '20 at 00:43
0

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.

Mark N Hopgood
  • 843
  • 8
  • 14
0

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. ;)

Gerard ONeill
  • 3,914
  • 39
  • 25
0

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

One 2021 Answer, which is certainly not the only answer as we see in this thread:

If you're using Visual Studio Code and using regions to block your code, then you must use # rather than // to define the region. To the question, No, even for this use case : If you are commenting out a region, you can use # or // or /** */, the technique you use for this is personal preference.

Examples for block definition in VSCode :

#region this is a major block
/** DocBlock */
function one() {}
/** DocBlock */
function two() {
  #region nested region based on indentation
  // comments and code in here
      # another nested region based on indentation
        // foo
      #endregion
  #endregion
}
#endregion

On Fold of the inner block:

#region this is a major block
/** DocBlock */
function one() {}
/** DocBlock */
function two() {
>  #region nested region based on indentation
}
#endregion

On Fold of the outer block:

> #region this is a major block

I cite the following specific usage which one might be tempted to try, but these do not work. In fact this is exactly how you DISable a #region block:

// #region
// #endregion
/** #region */
/** #endregion */

As to commenting out a region in VSCode:

/** You can now collapse this block
#region Test1
// foo
#endregion
// everything through to here is collapsed
*/

// #region Test1
// folding is disabled here
// #endregion

# #region Test1
// this also disables the fold
# #endregion

All of that said, "Is there any reason, aside from personal preference, to use // rather than # for comments?" I agree with comments in this thread and in the other thread: // is more commonly recognized and used, which is usually a good reason to use that comment style over #.

Final note, be careful about nesting based on indentation, as code formatting can remove your manual indentation and thus ruin your scheme of nested blocks based on comments. I've tested this with both # and // (which BTW, // nests on indentation too. Again, in context with the OP question, No, there is no reason to use // over # for nested indentation in this context in the current VSCode because both work exactly the same. However, this is a use case for using # over //.

Ref - no extension required, verified in 1.62.3. See notes on indentation there as well.

TonyG
  • 1,432
  • 12
  • 31
-8

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

Andre
  • 9