PHP Comments

When writing code in PHP, it can help you to keep notes next to specific bits of code so you can easily reference what a specific function does or pinpoint a specific place in your code. Another time comments are used is at the top of each PHP file in a script to leave the name, version and author of the script for anyone who may be using it in the future. Comments allow you to do this.

Comments perform no function at all. They are merely notes within your PHP code, which are ignored by the code itself. They are for your reference only.

There are two ways you can insert comments in your PHP code. Single line comments and multi line comments.

Single Line Comments

Single line comments are used to leave a very short note within your code that will fit on a single line. To leave a single line comment, the line must begin with two forward slashes to be completely ignored: //

You can also leave a single line comment after a function on a line, which will allow the function to operate while the comment which appears on the same line after the function will be ignored.

An example of using a single line comment can look like:


echo "John Robinson"; // This will display my name

This bit of code would display:

John Robinson

The double forward slashes represent the start of the single line comment, so everything that appears on the same line after the two forward slashes will be ignored.

To comment an entire line, insert the two forward slashes at the very beginning of the line:


// echo "John Robinson";

This bit of code would not display anything at all, as the entire line is nothing but a comment, which again, is ignored.

Multi Line Comments

To leave longer comments which you would like to spread out on multiple lines, you would use a single forward slash followed by an asterisk to begin the comments: /*

To end the comments, you would reverse the order, with an asterisk then a forward slash: */

An example of using a multi line comment can look like:


/*
Script name: John's Script ver. 1.0
Author: John Robinson
Script homepage: http://blog.triphp.com
*/
echo "John Robinson";

This bit of code would display:

John Robinson

Everything within the /* and */ would be ignored as a multi line comment.