views:

335

answers:

6

As far as I know, there are 3 types of comments recognized by PHP:

  • /* A block comment */
  • // A single-line comment
  • # Also a single-line comment

However, many coding standards, for example, those of PEAR or Kohana, discourage the last type of comments with no explanation. The question is, why is it so? Is this syntax planned for deprecation anytime soon?

+10  A: 

C-language comment style has become an industry standard. There's nothing wrong with using # comments at all unless the particular coding standard for your project or workplace prohibits it. Standards are the key to yielding readable code.

In many cases, // and/or /* */ are considered standard comment syntax for programming languages. The only exception that commonly is encountered is VB, which uses '. In contrast, # is often considered the standard comment syntax for shell scripting.

PHP was designed to perform both tasks originally. PHP can and does function reasonably well as a shell scripting language and can be invoked from the command line. It has functions to handle reading from stdin and writing to stdout. This is probably the origin of the # syntax for comments. # is probably discouraged because PHP is thought of as a programming language nowadays, rather than a tool for shell scripting. Specifically, the style guides that the question references are for web apps, rather than some shell tool.

David Pfeffer
That’s a very C-centric comment. Off the top of my head, I’d say that `//` and `/* */` are only used as comments in C-derived languages, and *no* others. Consider Ada, ASM (diverse dialects), BASIC/VB, Boo, Eiffel, Fortran, Haskell, Lisp/Scheme, ML/(O)Caml, Pascal/Delphi, PostScript, Smalltalk, TeX … the list goes on, even discounting anything remotely resembling a shell scripting language.
Konrad Rudolph
Most of the languages you mentioned are very uncommonly found in modern development. C and C derived languages (C++, C#, Java, PHP, JavaScript, etc. etc.) are among the most common languages encountered today, making C-like syntax an industry standard. The only popular exception that I can think of is VB.
David Pfeffer
Neither Ruby nor Python use C-style comments (both use `#`), and they're fairly popular, commonly-used languages.
mipadi
Both derive from scripting language roots though.
David Pfeffer
I guess, but you're kind of using a "No true Scotsman" argument to support the claim.
mipadi
A: 

maybe // and /* */ are also commenters for many other languages like c, c++, c#, javascript, css, and many more. so maybe people got used to using them. just guessing.

Funky Dude
A: 

I would think its because a lot of other programing launages use // as a single line comment (C,C++, C#, java, ect..) and PHP gets a lot its syntax from the C like languages.

I don't belive that # will be deprecation any time soon.

Steven smethurst
A: 

The only reason I can think of is that it would hurt code readibiltiy to have two different kinds of one-line comment syntaxes floating around. // seems to be the more common way, so I'm guessing they are just trying to limit themselves to one to increase readibility

GSto
A: 

The # comment form in PHP is discouraged by PECL style guides but is still acceptable and probably won't be deprecated. similar to the reason that class names usually do not have numbers in them. it's just convention

rabbit
+1  A: 

From the links you posted, this seems like a purely stylistic choice. PEAR and Kohana would like to have some unity when it comes to style and they have (possibly arbitrarily), chosen to prefer C-Style comments to UNIX/Perl-style ones.

Ben S
It is entirely stylistic.
David Pfeffer