In any programming language, commenting your code is important. This is especially true in PHP when you more than likely will have code being looked at by database managers, PHP developers, and sometimes the people responsible for maintaining your server. Comments let others know anything you need them to about the code. Sometimes it’s just the last time the file was updated or who updated it. Other times it will be to explain what a block of code or function does. Occasionally you may even use comments to store old code on a file. How you use comments is endless, but either way they should help you or whoever touches the file in the future, understand what is happening.
So how do you do it? Well there’s a few to go about it, there’s a single line comment and a multiline comment. A single line comment will comment out everything on the line after it. To do a single line comment you would do either two front slashes(//) or a hashtag or pound sign(#). A multi-line comment can span multiple lines. It can also be used inline and comment a block of text on one line, continuing code after it. To do a multi line comment, you would start with a front slash and then an asterisk(/*). To end the comment, you would do an asterisk and then a front slash(*/). Below is some examples of comments.
$variable=1; //This is a single line comment letting you know it set the variable to 1
$variable++; #This is a single line comment letting you know it set the variable to itself plus 1
$variable=3; /*This is a multi-line comment
The variable $variable is now set to 3*/
$variable=2; /*This is a multi-line comment used inline*/ $variable++;
There is another way to do single line comments. By using the hashtag (or pound symbol) or #, it is the same as using the double slashes (//). This is less common than the slashes because you would use slashes in C++, JavaScript, and other languages, so if people are familiar with those, they are more likely to do the double slashes.
#This is another way to do a comment in PHP
Comments are Underused
If you knew nothing about code, the comments above would help you decipher what was happening. Unfortunately comments are often underused (yes even by me). They may seem pointless at first, but the more advanced your code gets and the more people who work on it, they become very valuable. Even if you’re the only one who will ever see your code, you may do something today and not understand why tomorrow. When starting out in PHP, you should get used to making comments so that it is second nature when you are an experienced PHP developer.