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.
The reason comments are so handy is because they are only viewed by those looking at the source code of the original file- not the viewer of the website. Best yet, they aren’t even visible to those who try to view the source code of the website, since PHP code is hidden from the browser when being properly used.
Unlike HTML comments, PHP comments aren’t even visible in the source code of a website. HTML comments are visible to the general public, which can potentially lead to the stealing of code or may even help hackers exploit applications. PHP comments aren’t output to the browser at all, so they are completely safe from prying eyes.
When we wish to tell the PHP engine that we want to make use of a comment, we have three different structures to select from. The first two are “#” and “//” and are considered to be single line comment structures. For commenting on multiple lines, we use “/*” and “*/” in that order. Multiple line comments can be created through multiple single line comment calls, but this is of course redundant.
Unbeknown to most, PHP comments can also be used for more practical scenarios, such as troubleshooting. Expert programmers will find they have a problem with their application, and comment out different blocks of code to see what is causing the error. While it is usually in new code blocks, this method will indeed show that sometimes the problem is due to program code interacting wrong, which can in effect mean the problem is anywhere in the application.
Commenting in PHP is also great to use in selection structures, since PHP has long been known as a hard to scale language. Once files start getting big, it can be dizzying to try and remember which loops and selection structures go where, and what they do. By commenting out every closing bracket, and what it is in relation to, the problem is easily fixed. This is often mandatory for programmers who work for employers.
Closing Comments
The Internet is full of many examples of how to creatively use PHP comments. Try scouting out some websites for more information on the usage, concept, and execution of comments to get better experience with this blessing. As developers find, an early history with commenting in programming is always the best route.
Learn more about php code comments and how to comment out php.