[ Team LiB ] Previous Section Next Section

Adding Comments to PHP Code

Code that seems clear at the time of writing can resemble a hopeless tangle when you try to amend it six months later. Adding comments to your code as you write can save you time later and help other programmers more easily work with your code.

A comment is text in a script that is ignored by the PHP engine. Comments can be used to make code more readable or to annotate a script.

Single-line comments begin with two forward slashes (//) or a single hash sign (#). All text from either of these marks until either the end of the line or the PHP close tag is ignored. Here's an example:

// this is a comment
# this is another comment

Multiline comments begin with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/), as in the following:

this is a comment
none of this will
be parsed by the
PHP engine


The PHP Extension and Application Repository (PEAR) provides a growing collection of libraries and scripts for extending PHP's functionality. It includes a package called phpDocumentor, which can convert your inline comments into hyperlinked documentation. This is extremely useful for maintaining large projects. You can read more about PHPDocumentor at http://phpdocu.sourceforge.net. We cover PEAR and phpDocumentor in more detail in Hour 23, "PEAR: Reusable Components to Extend the Power of PHP."

    [ Team LiB ] Previous Section Next Section