Team LiB
Previous Section Next Section

Commenting Your Code

Commenting your code is a good habit to have. Entering comments in HTML documents helps you (and others who might have to edit your document later) keep track of what's going on in large documents. Comments also allow you to write notes to yourself during the development process, or to comment out parts of code when you are testing your scripts, so the code is not executed.

HTML comments are ignored by the browser and are contained within <!-- and --> tags. For example, the following comment reminds you that the next bit of HTML code contains a logo graphic:

<!-- logo graphic goes here -->

PHP uses comments too, which are ignored by the PHP parser. PHP comments are usually preceded by double slashes, like this:

// this is a comment in PHP code

But you can use other types of comments, such as

# This is shell-style style comment

and

/* This begins a C-style comment that runs
onto two lines */

Create a script full of comments so that you can see how they're ignored. Yes, I'm telling you to write a script that does absolutely nothing!

  1. Open a new file in your text editor.

  2. Type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Code Comments</TITLE>
    </HEAD>
    <BODY>
    <!-- This is an HTML comment. -->
    
  3. Type the following PHP code:

    <?
    // This is a simple PHP comment.
    /* This is a C-style, multiline comment. You can make this as
    long as you'd like. */
    # Used to shells? Use this kind of comment.
    ?>
    
  4. Add some more HTML so that the document is valid:

    </BODY>
    </HTML>
    
  5. Save the file with the name comments.php.

  6. Place this file in the document root of your web server.

  7. Open your web browser and type http://127.0.0.1/comments.php.

You should see absolutely nothing in your web browser, because all you did was print an HTML comment (which is ignored by the browser). Because the PHP parser ignores comments and the PHP block didn't contain any actual commands, there was no other output to display. If you view the source of this document in your web browser, you will notice that only the HTML comment is visible. Although the PHP code was all comments, it was still parsed and therefore is not visible to the users.

Click To expand

HTML and PHP comments are used extensively throughout this book to explain blocks of code. Get used to reading comments, and try to pick up the habit of using them. Writing clean, bug-free code that also contains comments and plenty of white space for easy reading will make you popular among your developer peers, because they won't have to work extra hard to figure out what your code is trying to do. In the next chapter, you'll learn all about variables, or, as I like to call them, "those things with the dollar signs."


Team LiB
Previous Section Next Section