Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - About Comments

About Comments - Images: Japanese

Hey there, everyone!

Next up, let's look at 'comments' and 'commenting out'. HTML has comments too, and in fact pretty much every programming language has a comment feature. Adding a comment or turning something into a comment is often called "commenting out." So 'comment' is the noun and 'comment out' is the verb.

Let's take a fresh look at what a 'comment' actually is. The syntax varies by language, but the one thing they all have in common is that the code inside a 'comment' is never executed.

So let's quickly refresh our memory on HTML comments. An HTML comment looks like this:

<!-- This is a comment. -->

Written this way, the content is treated as a 'comment' and won't be rendered by the browser.

However, there's an important caveat with HTML comments worth remembering. An HTML comment is just not rendered — it still exists as plain text in the file. That means if a visitor clicks "View Page Source" in their browser, they can read your comment right there.

<!-- HTML comments are visible to anyone viewing the source, so writing unreleased info like this is asking for trouble. -->
<!-- Hey engineers: please publish the following item on Feb 7. -->
<!-- <a href="/new.html">Dragon of Fantasy 12 releases on March 7!</a> -->

With a dynamic language like PHP, however, comments are never executed and therefore never output as text — so you can freely write unreleased info or anything else you'd like without it leaking to visitors.

<?php
// With PHP comments, nothing here is ever output as text. Feel free to write unreleased info, vent about your boss, or scribble whatever's on your mind.
// Hey engineers: please publish the following item on Feb 7.
// <a href="/new.html">Dragon of Fantasy 12 releases on March 7!</a>

So when building websites with PHP, it's generally a good idea to use 'PHP comments' rather than 'HTML comments' wherever possible.

Now let's go through the PHP comment syntax. PHP has two types of comments: 'single-line comments' and 'block comments'.

First, single-line comments. Adding '//' or '#' makes everything from that point to the end of the line a comment. As for which one is more common — it's probably about half and half, so feel free to use whichever you prefer.

<?php
// This is a 'comment'.
# This is also a 'comment'.

One thing to keep in mind with PHP single-line comments: a single-line comment ends at either a 'newline' or a 'PHP closing tag'. Pay particular attention to the behavior where a comment ends at the PHP closing tag — it's a PHP-specific gotcha that comes from its HTML-embedding design.

<?php
// This is a 'comment'.
// This part is a 'comment'. ?>But this is outside the PHP block, so it's NOT a comment. It gets output normally.

Next, block comments. These are for multi-line comments. Just wrap the content between '/*' and '*/' and that entire section becomes a comment.

<?php
/*
This is a comment.
You can span multiple lines.
*/
?>

This applies to other languages too, but be careful not to nest /* and */ inside each other. In the example below, the first */ encountered ends the comment, leaving a stray */ which causes an error.

<?php
/*
/*
Nesting like this will cause unexpected behavior.
*/
*/
?>

Also, unlike single-line comments, a block comment does NOT end at a PHP closing tag.

<?php
/*
A PHP closing tag does not end a block comment.
?>see
*/
echo 'Hello world';

Think of it as the '?>' being treated as part of the comment. Easy to get tripped up on, so keep it in mind.

And that covers PHP 'comments'. In the next article, we'll take a look at how PHP handles 'statements', whitespace, newlines, and tab characters. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .