1. Home
  2. Computing & Technology
  3. PHP / MySQL

How and Why to Comment Your PHP Code
Why You Should Comment Your PHP Code, and How To Do It

By , About.com Guide

A comment in PHP code is a line that is not read as part of the program. Its only purpose is to be read by someone who is editing the code. So why use comments?

  1. To let others know what you're doing. If you are working with a group of people, or plan on anyone else ever using your script, the comments let the other programmers understand what you were doing in each step. This makes it much easier for them to work with, and to edit if needed.

  2. To remind yourself what you did. Although at the time you may just be writing a quick script for yourself, and don't see the need for commenting, it is a good idea to add them in. Most programmers have experienced coming back to edit their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking, to avoid having to de-code your own workings.

So how do you add comments into your PHP code? There are in fact several was to add a comment. The first is by using // to comment out a line. Here is an example:

<?php
echo "hello";
//this is a comment
echo " there";
?>
If you have a single line comment, another option is to use a # sign. Here is an example of this:
<?php
echo "hello";
#this is a comment
echo " there";
?>
If you have a longer, multi line comment, the best way to comment is by using /* */. You can contain several lines of commenting inside a block. Here is an example:
<?php
echo "hello";
/*
Using this method
you can create a larger block of text
and it will all be commented out
*/
echo " there";
?>
More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn PHP
  5. PHP Comments - PHP Commenting - Comment Code PHP

©2009 About.com, a part of The New York Times Company.

All rights reserved.