Commenting PHP Code

From Techotopia
Revision as of 16:41, 24 May 2007 by Neil (Talk | contribs) (New page: 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 P...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

Commenting of code is often neglected by software developers. Sometimes thi sis because the code is being developed to meet a looming deadline and there is no time to adequately comment it. Often there is a tendancy on the part of the developers to believe that they will remember how the code works six months or a year from now. Another common excuse for not commenting is that the code is so well written as to be completely self-explanatory.

Excuses aside, there is much to be gained from included helpful and concise comments with the PHP code 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 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 have to continue to maintain your PHP scripts long after they are written.

Another important reason for commenting your code is to ensure that others who may follow in your footsteps to maintain or add functionality to your creation will also benefit from reading your comments. There are few things worse in the software development business than having traverse the steep learning curve caused by the complexity of somebody else's uncommented code.

Now that we have established the commenting code is worthwhile we can take a look at the mechanisms provided by PHP to achieve this goal. PHP provides two commenting mechanisms - one for single line comments, and another for multi-line comments.