Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-line comments vs single line comments

Tags:

comments

php

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?

like image 548
Sean Avatar asked Mar 12 '12 20:03

Sean


People also ask

What is the difference between single line and multiline comment?

There is no difference. It comes down to user preference. However, special variations on those syntaxes are interpreted by Javadoc for generating documentation.

What is comment line give single and multiline comment line?

We looked at single-line and multi-line comment in C#. Single-line comments end at the first end-of-line following the // comment marker. You can place it at the top of the code statement or after the code statement. If it's after a code statement, whatever text following it is regarded as the comment.

What is single line comment and multiline comment Python?

"""This is a multiline comment in python which expands to many lines""" Here we have to keep in mind that the multi line comments are only string constants that have not been assigned to any variable. So they have to be properly intended unlike single line comments with # symbol so that syntax errors can be avoided.

What is the example of single line comment?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by the compiler (will not be executed).


1 Answers

is it fine to use single line comments in such situations?

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:

$var = 'Hello'; // Say hello
echo $var;

Upload to UNIX server with Windows style \r\n newlines:

$var = 'Hello'; // Say helloecho $var;

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.

like image 90
Wesley Murch Avatar answered Sep 20 '22 06:09

Wesley Murch