- 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.
- 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:
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"; #this is a comment echo " there"; ?>
<?php echo "hello"; /* Using this method you can create a larger block of text and it will all be commented out */ echo " there"; ?>