Website Creation with PHP Comments

In most, if not all, programming languages, there is a way to make a note about what a particular piece of code is doing, why it is necessary, or anything important related to that code. We call these notes "comments" and you should use them when necessary when creating websites. If you are wondering if it is necessary and think there is a chance it might be, you should make a comment. When you or someone else comes back and tries to understand what's going on, this can be very helpful.

However, there are at least two potential drawbacks when creating comments in your code when creating websites. The first would be writing comments that are so numerous and unhelpful that they make others unlikely to read any comments at all. Make comments are that concise and relevant to the code or problem beneath or beside it. The other problem with comments is that they can easily become outdated or just plain wrong. If you are updating or changing code, consider reading the comments and correcting them as necessary around the code you are changing.

With all of those disclaimers and introductions aside, we can start demonstrating how to use comments in PHP. Typically, programming languages give you multiple ways to make a comment. One or more for commenting a single line. These are very useful for concise explanations and are the most commonly used type of comment because as mentioned above, excessive commenting simply results in less useful comments that might just end up being ignored. Below is an example of a comment in PHP. Also, notice PHP has two different ways to comment a single line.

//echo "This is a comment";
#echo "This is also a comment";
echo "This is NOT a comment";

The other type of comment is a multiline comment. While they shouldn't be used too frequently, they are particularly useful when dealing with a difficult problem or explanation a complicated context of why this code exists when you created the website. These can explain difficult business cases or give insights to why the code had to be unintuitive and looks and behaves the way it currently does. Let's take a look at an example of a multiple line comment in PHP.

/* this
entire thing
is a comment */

So anything between /* and */ is commented or anything that follows // or # on the same line.