When programming in any language the process of adding comments involves writing notes alongside the code to describe what the code does and how it works. The comments are ignored by the PHP pre-processor when executing a script and are purely for human consumption.In Short Comments are used as a note of the code for the help of programmer and the web developers who may view want to view source code |
|
Excuses aside, there is much to be gained from included helpful and concise comments with the PHPcode that powers your web site. Firstly, you will be amazed at how puzzling a section of code can be even a few months after you have written it. |
|
It is not unusual for a developer to revisit some old code they once wrote and express amazement that they actually wrote it. It is important to remember that there is a good chance you will have to continue to maintain your PHP scripts long after they are written. |
|
In PHP, we use // or # to make a single-line comment and /* and */ to make a large comment block. |
|
PHP Single Line Comments(//,#) |
|
Comments that reside on a single line are prefixed with the two forward slash characters in PHP (i.e //). |
|
The following example contains a single line comment: |
|
<?php
// This is a single line comment
eg:<?php
echo "This is to be shown";
//This will Not be shown on the browser.
#This will also not be shown on the browser.
?>
?>
|
|
The single line comment can be on a line of its own, or it can be appended to the end of a line of code: |
|
<?php
echo "This is a test line"; // Output a line of text
?>
|
|
In the above example the PHP pre-processor will execute the echo statement and then ignore everything after the // single line comment marker. |
|
Single line comments are also useful for temporarily highding the lines of code from the execution. For example, the following change to our previous example will cause the PHP pre-processor to ignore the entire echo command during execution: |
|
<?php
// echo "This is a test line";
?>
|
|
PHP Multi-line Comments |
|
Multi-line comments are wrapped in /* and */ delimiters. The /* marks the start of the comment block and the */ marks the end. The following example demonstrates the use of multi-line commenting in PHP: |
|
<?php
/*
This a multi-line block
of comments and this would not be displaed on browser.
*/
?>
|
|
Multi-line comments are useful when you have notes you want to make in the code that will take up more than one line. The ability to mark blocks of lines as comments avoids the necessity of placing the single line comment marker at the start of each comment line. |
|
Another useful application of multi-line comments is to comment out blocks of PHP code temporarily. It is quite common to have written some PHP script and then wonder if you can re-write it so that it is perhaps more efficient or reliable. |
|
In this situation you can comment out the old script fragment so that it is no longer executed and write some new code. If it turns out your new code is worse than the original (which happens from time to time) you can simply remove the new code and uncomment the old to bring it back into the execution flow. |
|