Comments

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.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Conditional statements >>

Previous chapter: Opening and closing code islands

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.