PHP Comments Tutorial

Foreword: Arrogance runs thick among the LearnPHPOnline.com programmers- we hate doing more work than we have to! Commenting is something that is normally ignored by new or lazy programmers, but both groups soon find commenting saves much more time than it takes.

Comments may only viewed by those who make changes to the code itself- the PHP engine doesn’t parse anything designated as a comment. A good example of this is seen below, where the first line isn’t being output to the browser, while the second is.

An Example Of The PHP Comment Syntax

<?php

// This is an example of a comment- this is not being parsed!

echo "This is being parsed by the PHP engine!";

?>

We, as able-bodied programmers and individuals, should invest our time into commenting for three primary reasons.

  1. Employment – If you are working for an employer, or are planning to release your source code to the public, you will need to make use of comments. Employers demand it because if they were to lose you as an employee, they would have to hire someone new to peruse your code. This act with commenting can take hours, and without can take days. This same principle works with open source code, which needs to be readable by others so they know how to operate or modify your program correctly.
  2. Saving time – Even if you plan on keeping the code to yourself, you should make use of PHP comments because as your projects grow in scale, you will forget where you place certain bits of code or even what large blocks of code do. Most arrogant programmers do without comments at this stage, claiming they can remember the programs they make. In reality, a programmer will often come back to a certain code block months into the future, and quickly find they will need to take a few hours to reread their code and comment it properly.
  3. Troubleshooting – More intelligently, programmers use PHP comments to troubleshoot their applications. When trying to find which code block is causing an error, programmers can quickly section out new additions to the application with a comment block to see if the error subsides. If it does, that’s where the error lies. If not, the error lies elsewhere.

PHP Commenting Syntax

You’ve finally come around, and you want to start commenting your code. Bravo! The first step is to learn how to use it. There are three different ways we can make comments; two of which can be seen below in the example.

Two different Methods of Using PHP Comments


<?php

#This is an example comment

//This is another example comment

echo "Sentence 1."; //This is our first statement

echo "<br />"; #This is a line break

echo "Sentence 2."; // This is our second statement

?>

You’ll notice that both commenting styles, either the “#” or the “//” versions, are used on a single line. Also note we can use both behind an otherwise functional PHP statement, such as can be seen with our second half of the example. But if we wanted to format comments to span multiple lines, things would get tedious. Luckily, PHP developers can make use of a third way that spans multiple lines as seen below.

Multi-Line Comments in PHP


<?php

#This is a

#single line comment

#on multiple lines.

/* This is an easier

way to write comments

on multiple lines */

?>

In the above PHP commenting example we see that you can span multiple lines both ways, but the latter example is much easier to work with. These types of multiple line comments are usually placed at the top of a PHP file to explain what it’s for, what it accomplishes, and any specific information needed to run or edit the file correctly.

Some people like to showoff and get elaborate with their commenting styles. We’ll agree it’s great fun, since it actually helps out the readability of comments in many cases. Below is an example of what the LearnPHPOnline.com’s creative programming department came up with in all but a few seconds of their precious time.

Fancy-Shmancy PHP Comments


<?php

#################################

####  My PHP File Version 2.2####

#################################

/* Be sure to edit the server 

password and username details 

to properly configure this 

script to work! */

?>

Quite chauvinistic aren’t they?

Using PHP Comments To Troubleshoot

Troubleshooting is a vital part of programming, as we are sure the reader knows already. It’s always a great idea to take breaks every time a new code block is added to see if everything is still functional. If it isn’t, we have to get creative in how we find the root of the problem.

Below is an actual flawed script that we created. You will likely see the problem right off, but in a real-world environment, tracking such a problem down will be much more difficult.

Troubleshooting PHP Code With Comments


<?php

//This script will output a variable to the browser

$myVar1 = " meow mix";

$myVar2 = " meow mix';

echo "I want chicken, I want liver," . $myVar1 . $myVar2 . " please deliver!";

?>

When run, this script will give the error “Parse error: syntax error, unexpected T_STRING” – and even though the problem lies on line 4, it points us to line 6. So we’ll just comment out line 6, and we see that the problem still persists!

Troubleshooting PHP Code With Comments


<?php

//This script will output a variable to the browser

$myVar1 = " meow mix";

$myVar2 = " meow mix';

#echo "I want chicken, I want liver," . $myVar1 . $myVar2 . " please deliver!";

?>

Oh dear, it looks like the problem is still there once we run the application.

Now we’ll move the comment to line 4, and see what happens. Once we run the application, we see that the browser outputs this string “I want chicken, I want liver, meow mix please deliver!” We are missing the second meow mix, obviously, but the script still executed successfully. This means that the problem must lie on or near line 4. Upon further checking, we see that we used an apostrophe to close the variable assignment! Oops! Let’s fix it and see if it runs now.

Troubleshooting PHP Code With Comments


<?php

//This script will output a variable to the browser

$myVar1 = " meow mix";

$myVar2 = " meow mix";

echo "I want chicken, I want liver," . $myVar1 . $myVar2 . " please deliver!";

?>

Success! We now have the correct string output to the browser.

One Last Tip For PHP Comments

One last thing we need to stress is to use PHP comments in selection structures, such as CASE or IF. If you have an IF statement, always be sure to comment what each branch does, even if you don’t use one of the choices. An example is below.

PHP Comments And Selection Structures


<?php

//This script showcases comments in an IF structure

$clientAge = 18;

if ($clientAge < 18) {

echo "You are not able to view this material until you are 18 or older.";

} else {

//Don't show a notice

}

?>

Even though we don’t need a comment in the else branch, we should put one there because it does amazing things for readability. Once programmers start nesting IF statements inside each other, documenting each IF branch is absolutely vital in understanding how an application functions.

Closing Comments

Commenting is a vital part of programming, and it serves more purposes than what one would think as we can very well see. Always try to keep documentation on your code where possible, if not for yourself, then for your employers who don’t like seeing undocumented work from their employees.

Comments
  1. Adegboyega
    August 25, 2009

    Nice lecture

    Leave a reply
  2. Pankaj
    September 8, 2009

    It’s amazing style of understand easily, basically these comment have mentioned very good.

    Leave a reply
  3. hosterzru
    November 27, 2009

    Another great list. I’m meeting more friendly and helpful bloggers each week. Great Idea ;)

    Leave a reply
  4. Iurie
    May 22, 2010

    Do u have any tutorials for beginers ? I am reading a php-book for 2 months already but cant understand how to create a coment form and contact us form :( ((

    Leave a reply
Leave a Comment Below »
Your Name
Your Email Address
Your Comment