fb

Ads

Pages

Inline Comments [PHP]

Inline Comments

For inline code comments, PHP supports three syntaxes:




C-style comments—With this type of comment, everything between /* and */
is considered a comment. Here’s an example of a C-style comment:


/* This is a c-style comment
* (continued)
*/
 


C++-style comments—With this type of comment, everything on a line following // is considered a comment. Here’s an example of a C++-style comment:

// This is a c++-style comment


Shell/Perl-style comments—With this type of comment, the pound sign (#) is the comment delimiter. Here’s an example of a Shell/Perl-style comment:
 

# This is a shell-style comment
 

In practice, I avoid using Shell/Perl-style comments entirely. I use C-style comments for large comments blocks and C++-style comments for single-line comments.

Comments should always be used to clarify code.This is a classic example of a worthless comment:


// increment i
i++;


This comment (//)  simply reiterates what the operator does (which should be obvious to anyone simply reading the code) without lending any useful insight into why it is being performed. Vacuous comments only clutter the code.
In the following example, the comment adds value:


// Use the bitwise “AND” operatorest to see if the first bit in $i is set
// to determine if $i is odd/even
if($i & 1) {
return true;
}


This comment explains that we are checking to see whether the first bit is set because if it is, the number is odd.

0 comments:

Post a Comment