30

Lately I've been using # instead of // for doing single line comments inside my code. However, I see most of the people prefer //.

Is there a particular reason for preferring them instead of #? Performance? File weight? Anything?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MarioRicalde
  • 9,131
  • 6
  • 40
  • 42
  • [Support for prefixing comments with # in INI files has been removed](http://php.net/manual/en/migration70.incompatible.php) in PHP 7. May be # commenting is going out of fashion. Better to stick with popular // – nawfal Nov 12 '15 at 06:15

9 Answers9

25

It doesn't change a single thing, I'd say ; it's just a matter of habits :

  • // comes from C, and exists in several languages used by lots of people.
  • # comes from shell and Perl, and exists in not as many "important" languages as // -- so less people use it.

And why are those two both available in PHP ? Well, PHP has been built with people knowing both C, Shell, and Perl, and they have brought what they liked from those languages ;-)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    well, let's just be logical: if you type // 5000 times in a year, you're saving yourself one keypress if you use # instead. Save your joint, don't be religious about something that doesnt matter. Otherwise, you can also setup a macro on your keyboard (ex: Logitech G910) where you can press one key and it prints out // + space automagically. – Joel May 30 '16 at 16:33
  • 4
    @Joel - Don't you need to press two keys to get # too? ;) – Krzysztof Przygoda Nov 07 '16 at 20:09
  • 2
    @KrzysztofPrzygoda no my keyboard is set on "French Canadian" so it's just one key (no CTRL or shift) just above the TAB key. And that's fantastic. – Joel Dec 04 '16 at 03:44
  • I am typing `/** */` or `/// ` for a while now ([Doxygen](https://en.wikipedia.org/wiki/Doxygen) / [phpDocumentor](https://docs.phpdoc.org/3.0/guide/guides/docblocks.html) style). IDEs help to type those, so no reason not to. – Peter Krebs Jun 10 '21 at 14:49
  • This should be updated since Python has become so prevalent I would say its a very important language, and now many people are used to the # comments – Samwise Ganges Feb 15 '23 at 23:19
6

I'd probably stick with // simply because it's the same in JavaScript, and when you code PHP, you're likely going to have some JavaScript in your files as well. (Most websites use JavaScript nowadays.)

This way, your comments look the same, regardless of the language, and it'd ease readability a small bit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steffen
  • 13,648
  • 7
  • 57
  • 67
4

For single line comments, there is no significant technical reason for prefering // over the octothorpe (that's what the Zend PHP 5 Certification Study Guide calls #) or vice versa. // seems to the more common choice out there in the wild.

Asaph
  • 159,146
  • 25
  • 197
  • 199
4

I would only suggest you use // or /* */. As your programs grow you may wish to use a documentator like phpdoc to automate documentation for your program and most of these documentators as well as IDE's will only accept these two.

yannis
  • 694
  • 11
  • 17
3

I think it's merely a matter of taste and preference. The # as comment marker has its roots in shell scripts while // goes back to C++ so depending on the people who read your code and their background one or the other might look unfamiliar.

Joey
  • 344,408
  • 85
  • 689
  • 683
3

You can use #, but // is much more common. It is also the agreed upon Code Convention in PEAR.

Gordon
  • 312,688
  • 75
  • 539
  • 559
2

IDE support may be a reason. For instance, with Eclipse you can automatically comment and uncomment blocks with //, but not with # (with the PHP plugin; the Python plugin does #). Looking at it that way, it depends on the IDE you and your collaborators use.

Confusion
  • 16,256
  • 8
  • 46
  • 71
1

"//" seems more consistent with the "/* */" block comments to me. But apart from that, I know of no reason to prefer one over the other.

selfawaresoup
  • 15,473
  • 7
  • 36
  • 47
0

Both of them are serving the same exact purpose: commenting out some piece of code. I'd prefer # because it takes 1 byte. // takes 2 bytes. ;)

Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35