PHP Tutorial: Part 7 – Adding Comments and Making Shortcuts

October 5, 2021 / Web Hosting
Making Shortcuts

In the seventh and final PHP tutorial of this series, we will look at how to add comments to PHP scripts and explain an important shortcut that will save time and help reduce errors.

Adding comments to PHP scripts

Although not essential for a PHP script to function, sometimes it is helpful to add comments so that you and other people who work on the script can have a better understanding of what is taking place and why. This can be very useful if you are in the process of developing the script or if it is being updated at a later time.

There are two ways you can comment on a PHP script. One is for single-line comments and the other for longer comments that stretch beyond a single line.

A single-line comment is written as follows:

// Your comment goes here

The double slash is a piece of code that tells PHP to ignore everything directly after it on a single line of script. You can place these comments on a separate line as above, or put them at the end of a piece of code, as in the example below:

print “Hello $name”; // Welcome to the user

A multi-line comment uses a different combination of symbols in the code. It puts /* before the comment and */ after the comment, for example:

/* The following piece of code will take the input
the user gave and will check that it is valid before
adding it to the database */

In this instance, anything between the /* and the */ will be ignored by the script. However, it is important to ensure you always close this type of comment using the */ as not doing so could make the script fail to work.

Shortcuts for outputting HTML to a browser

During this series of tutorials, we have used four different ways to output information to the browser. These are:

echo(“Text here”);
echo “Text here”;
print(“Text here”;
print “Text here”;

All of these options essentially do the same thing and achieve the same result, so it is quite possible to use any of them and even use different ones within a single script.

As both PHP and HTML use quotation marks, to avoid confusion between HTML and PHP during execution, it is important that any quotation marks used in the HTML code are replaced with \”. If you have a long script, changing all these could be an arduous task and there is the risk of not spotting them all. However, if you created the header and footer of a page dynamically using PHP and then had a static page in between, you could do the following:

Top PHP code in here
Bottom PHP code in here

In this way, by separating the PHP and HTML elements of the page into distinct sections, there is no need to change all the HTML quotation marks to \*. You can keep the sections apart and the PHP code will just continue from where it was left off after the HTML ends.

You could also do the following using IF statements and loops:

IF Statement {
HTML For IF Being Correct
} else {
HTML For IF Being Wrong

You must always remember to close IF statements and loops, though, as it is very easy to forget and the script will not work if you don’t.


Hopefully, this tutorial will have shown you how to add comments to your PHP scripts without them interfering with the way the script runs and also explained how you can break a script up into its PHP and HTML components to stop you from needing to change HTML quotation marks to \”.

Overall, we hope you have found the seven PHP tutorials in this series a useful introduction to coding in PHP and discovered ways you can use PHP script to help make your website better.

For more information about our hosting solutions for PHP based platforms, like WordPress, visit our homepage.

Spread the love