Monday, December 29, 2008

Saving Time By Using Comments In PHP Programming

By Chris Channing

Because of how PHP is structured, which is to say it is done so that anyone can use it according to their own style, developers don't have to make use of PHP comments. But the truth is, most professional web developers, if not all, stress that making use of comments in PHP is vital in keeping organization and improving readability for future use.

Comments aren't parsed by the PHP engine, so they are only visible to those who are viewing the original source code of the file. This is great for documenting what each code block does, all while keeping the casual visitor to a website oblivious to the extra comments present on the application they are using.

It's often a better idea to use PHP comments in place of others such as HTML comments, since PHP comments will never be seen by the end user if the script is running correctly. This simple fact will help safeguard against others stealing source code, ideas, or principles from an application.

Comments are created with three different calls. For comments that span two or more lines, we have to use "/*" and "*/" - and "#" or "//" for single line comments. Comments in PHP can actually be placed directly after real PHP code, above it, or below it. This leaves a lot of creativity in how developers document their code according to their style and tastes.

When problems arise, and they do arise often, developers need to find out ways to solve the problem and continue with their development. This process, troubleshooting, can be done with commenting as well. By commenting out new and old code blocks alike, we can determine which blocks of code are throwing errors and which are considered "clean" for usage. This is actually used quite commonly, despite most thinking comments are only useful for documentation.

One big use of commenting in PHP is to use comments in selection structures, whereas comments are placed in every possible choice given. Often, developers close selection structures, such as the IF structure, without commenting what each choice does since we don't always use every choice. But by defining each choice early on, this saves quite a bit of time down the road once developers start nesting loops and selection structures inside each other.

In Conclusion

PHP Comments are quite handy as we can see, more so than most would think. From troubleshooting to simple documentation, the three methods of commenting have a lot of use to the proper programmer. For more information on comments in PHP, and in other languages, check out more books, articles, and magazines for a more detailed guide in using them effectively.

About the Author:

No comments: