Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.7     Comments

This is NOT the latest copy of this book; click here for the latest version.

While in PHP mode, you can mark certain parts of your code as a comment that should not be executed. PHP has three ways of doing this: //, /* */, and #. // and # mean, "ignore the rest of this line", whereas /* means "ignore everything until you see */". Some complications exist with /* and */, though, which make them less desirable to use.

<?php
    
print "This is printed\n";
    
// print "This is not printed\n";
    # print "This is not printed\n";
    
print "This is printed\n";
    
/* print "This is not printed\n";
    print "This is not printed\n"; */
?>

That chunk of code shows all three types of comments in action, but does not demonstrate the problem with the /* */ form of commenting. If you were to start a /* comment on line one, and end it on the line near the bottom where the other /* comment is started, you would find that the script would fail to work. The reason for this is that you cannot stack up, or "nest" /* */ comments, and attempting to do so will usually fail spectacularly.

It is generally best to stick to // for your commenting purposes, simply because it is easier to spot, easy to read, and easy to keep control of.





<< 2.6.6 Opening and closing code islands   2.6.8 Conditional statements >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

WATCH OUT!! when using the one-line comment style.

As the PHP manual puts it: "The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first"

This means that the line

<h1>This is an <?php // echo 'simple';?> example.</h1>

will parse to valid html, even though you wouldn't expect it if you have a C background. More of a bug than a feature IMHO.

A Fortran77 User ;o) - 06 Sep 2008

Commenting of your code can be used even far more effectively by combining it with a code documentation generator. People who have worked with C/C++ and specifically when programming with QT will most likely know of doxygen. (http://www.stack.nl/~dimitri/doxygen/)

Similar tools exists for PHP. The one I'm using currently is phpDocumentor (http://www.phpdoc.org/).

These tools use the /* */ convention, where a to be documented code comment should start with /**.

Here is an example of such a documentation comment:

/**
* Does action foo
*
* This function performs action foo using $arg1 to
* select status bar.
*
* @param string $arg1 Some parameter to use when performing foo.
*/
function doFoo($arg1)
{
// Perform some action
}

Cheers,
Luud Heck



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow