Real World PHP

About   |   EN   |   ES

004 Comments

Sometimes while you are programming, you want to include descriptions or notes that you don’t want the PHP interpreter to parse. PHP lets you do this through the use of what are known as comments. Comments are super helpful to programmers who have to read code. For example, sometimes you need to clarify a complicated bit of code or leave a note to a future programmer who will be reading the code. The point is, comments are for people, not for computers.

There are different types or styles of comments that PHP supports.

Single-line comments:

<?php
// A single-line comment starts with two slashes.
// Everything after the two slashes is ignored by PHP
# A pound sign or hash symbol also works just like the two slashes,
# but two slashes are the most common comment.
?>

Multi-line comments:

<?php
/* Multiple line comments are surrounded with a slash and asterisk */

/*
  A multiple line comment will let you
  put as many lines of text as you want.
  It begins with a slash and asterisk 
  and then ends with an asterisk and slash.
*/
?>

Example

<?php
// This line is ignored by PHP
echo "Hello, world!";

echo "Cool."; // This comment is on the same line as code

# This line is also ignored.
echo "Neat";

/*
 * I don't need an asterisk on this line,
 * but it looks nice when there is an
 * asterisk on each line of this multi-line
 * comment.
 */
echo "Good bye.";
?>


Challenges

Commenting fun

Add some comments to the code you wrote before and run it. Notice how the PHP interpreter completely ignores it.