How and Why to Comment Your PHP Code
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?
- 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:
<?phpecho "hello";//this is a commentecho " there";?>
If you have a single line comment, another option is to use a # sign. Here is an example of this: <?phpecho "hello";#this is a commentecho " 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: <?phpecho "hello";/*Using this methodyou can create a larger block of textand it will all be commented out*/echo " there";?>