Comments in PHP: PHP comment syntax and examples

It's funny, when I first started working with PHP, the first thing I wanted to know is "What is the PHP comments syntax?" (It reminds me of learning to ride a motorcycle, where the first thing you want/need to learn is how to stop.)

Fortunately, if you have a Unix/Linux and/or C programming background, the PHP comment syntax will seem very familiar, and I'll share some simple PHP comment examples here.

One-line PHP comments with # or //

If you're familiar with Unix or Linux, you'll be glad to know that the one-line PHP comment syntax uses the usual # or // comment characters, as shown in this example:

# this is a one line php comment

// this is also a one-line comment in php

You can create PHP comments by starting a line with these comment symbols, and just like Linux shell programming and several other programming languages, you can also use these same comment symbols to put comments at the end of any valid PHP statement. Here's a quick example of how to do that:

$foo = 1;  // this is a php comment
$bar = 2;  #  another php comment

Multiline PHP comment syntax

You can also create multiline comments in PHP using the following comment syntax, which again you may have seen in many other programming languages:

/*
 * this is a php comment
 */

I often use this comment syntax above my PHP functions, like this:

/*
 * this function does yada yada yada.
 * it's very good, and has no known bugs.
 */
function do_something () {
  // my code here
}

I hope these simple examples of comments in PHP are helpful.