I remember reading in a JavaScript tutorial that it’s generally best to avoid using single line comments incase someone in the future decided they wanted to compress the file and remove all the white space.
Does the same ring true in PHP? Should I use multi-line comments even when I’m only going to be occuping one line, e.g.
echo $var; /*This code echoes a variable*/
or is it fine to use single line comments in such situations?
And are PHP files ever compressed by removing white space, or is that a little extreme?
Short answer: Yes.
If someone compresses whitespace and breaks your code, it’s their fault for not doing it right.
Any PHP or JS source compressor worth using should be smart enough to know what to remove and what not to touch, and all comments will generally be stripped anyways so it’s not a related issue. You shouldn’t be coding around such things.
The only practical reason I can think of to avoid single line comments is that sometimes there are issues with line endings when you go to/from a Windows and *NIX server. If you’re using Windows style newlines and your file retains them on a *NIX server, your single line comments will end up commenting out your actual code, messing everything up.
/* Comments like this */won’t affect anything. Example:Upload to UNIX server with Windows style
\r\nnewlines:You could end up executing some unintended, dangerous code if you run it.
However: If that happens, it’s a symptom of another problem that should be resolved anyways. I’ve had this happen to me a few times, so I know it’s possible. Many FTP clients will convert newlines anyways.
So in reality: Yes, it’s fine to use single line comments. Comments are for the author, not the compiler or parser.