How To Add Comments To Your PHP Program

How To Add Comments To Your PHP Program
Every programmer has his or her opinion as to what information should be included with the program code. Most programmers add comments with their code in order to remind themselves, and anyone else, what the code does and how it does it. When deciding what information to include ask yourself what you would need to be told about the program if someone else had written the program and now you were asked to run and maintain it.

Most of the programs that you write will have more than one page of code. Each page of code will be saved as a PHP program file. (filename.php3) There are two places in the program file where you will want to add some informational comments.

  1. At the top of each program file you will include general information about the program.

    • At the very top of the file and just after the opening PHP tag you will put the name of the file such as HelloWorld.php3.
    • Next you will add a copyright notice naming yourself as the copyright owner of the program.
    • If your program will have more than one page of code, you will mention that this is one page of a larger program and give the name of that program.
    • You will also want to include a brief description of the purpose of the program and how it works.
    • Last you will add any disclaimer and licensing information. For example this could be a license written by your lawyer or the GNU General Public License which you are free to use on any of your freeware programs. You can download a copy of this license from the Free Software Foundation website. We will discuss this in more detail in a separate tutorial.

  2. The second area where you will want to add comments is throughout the code itself at the beginning of each major section of code. Include a short comment about what task that particular section will perform.
There are three ways to add comments to your program code.

// comments go here
This is the basic PHP style comment. Anything on the same line and that comes after the two // will be ignored by the server and not processed as program code.

# comments go here
This is the shell-style comment. Anything on the same line and that comes after the # will be ignored by the server and not processed as program code.

/*
comments go here
comments go here
comments go here
*/
This is the C-style comment. Anything that is between the opening /* and the closing */ will be ignored by the server and not processed as program code. You can use this style when you have more than one line of comments such as we have discussed in this tutorial. Click here for an example.






This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.