<< PHP Variables | PHP Tutorials | PHP Output >>

PHP Comments

Yeah that's right PHP comments. If you're new to the whole 'programming' thing, you probably have no idea what a comment is and what it's used for. Well that's why I'm here of course :) A comment in PHP is code that the parser will ignore completely when executing your script. You might ask, 'Then what is the point of having them?'. Well simple, let's you organize your code better. You can label specific parts of your code so you don't forget what it does, or so other people can understand what it does.

Comment Syntax: Single Line

There are two ways to use comments in PHP. Single line or multiple line (or block comments). A single line comment can be expressed in two ways. The first (and most common) is two forward slashes right next to each other (or for people who like big words, consecutive). This can be shown like so:

<?php
// PHP parser will ignore me :3
?>

So to the PHP parser, that looks like a start and end tag and nothing else. As I said above these are single line comments. That means anything after them on the rest of the line (not anything above or below) will be commented. The other syntax is as follows:

<?php
# PHP parser will ignore me too :D
?>

That will also be ignored.

Comment Syntax: Multiple Lines/Block Comments

The next type of comment (and final) is the multiple line/block comment. This is useful when you have more than one line that needs to be commented, and you don't want to use a hundred forward slashes! Cause that would just totally suck. Now the syntax for multiple line comments is simple, it starts out like this /* and ends like this */. Anything between those two ... err ... 'things' will be ignored by the parser. Here is an example:

<?php
/* I am totally ignored. */

/*
Me too!!
*/

/* This is
ignored too.
*/
?>

All of the above will be ignored by the PHP parser.

Do It To It

Yeah when I first learned PHP, the tutorial didn't include the bit about the fact that you should use comments. BIG MISTAKE THERE. Most of the code I wrote in the past didn't include comments -_- Not very smart. Anyone else who reads it has no idea what is going on in the mind of yours. It could make great sense to you, and none to someone else. Commenting allows you to get ideas across to other users and programmers. You should make the effort to do it, and you can always make it fun :) Here is an example of a good commented script:

<?php
// Print 'Data'
echo 'Data';

/*
We've already printed 'Data'
Now let's print 'Is Cool'
*/
echo ' Is Cool';
?>

What the end user sees:

Data Is Cool

Even though that script wouldn't need commented because it's so simple, it provides a good example of what you should do.

<< PHP Variables | PHP Tutorials | PHP Output >>