Comments on Code

Posted on 23. Feb, 2010 by admin in Basics


So, PHP code is written for computers to understand what we want them to do, so what about us? That is where comments come in. Comments are basically just a line or two that explains what’s going on in a portion of your code. It can also be used to void a portion of your code that you do not want to run. There are two types of comments: one for single line short comments, and another for longer comments that span multiple lines.

The short single-line comments are made by adding // in front of the comment.  Multi-line comments begin with /* and end with */. Below are examples of both:


// This is a comment

/* This is a
comment that spans
multiple lines */

Comments are a pretty simple concept, but it takes practice to get them right. They are unnecessary for every line of code, but may be used every once in a while to explain what that section of the code does. Take a look at the next example and try it out on your website:


<html>
<body>

<?php
//This prints “Hello World” to the screen.
echo "Hello World";

/* This is another type of comment
that can span
multiple lines.*/
?>

</body>
</html>

See how the single line comment explains what the line below it does? We will cover proper commenting in other tutorials later.

Note: Anything that is “commented out” will be ignored by your server and not run. For example, if you put // before the echo statement in the above code, nothing will be displayed on the screen.

See how the single line comment explains what the line below it does? We will cover proper commenting in other tutorials later.

Note: Anything that is “commented out” will be ignored by your server at not run. For example, if you put // before the echo statement in the above code, nothing will be displayed on the screen.

2nd Note: You don’t need to put a semicolon(;) at the end of the comment.