PHP Comments Can Be Written Using Hash Tag
A Long-Overdue Discovery
May 21, 2025 • Experience
As a developer, I have always had my fair share of "a-ha" moments – those instances where a simple, seemingly insignificant detail opens up a world of new possibilities and understanding. One of the most recent ones that genuinely took me by surprise was the discovery that PHP, a language I have been working with for years, allows comments to be written using the # symbol. Yes, it's true! It took me several years before I realized that PHP comments are not only confined to the traditional // for single-line comments or /* */ for multi-line comments, but that the # symbol could also serve as a comment delimiter.
In this article, I’ll walk you through my experience, share some insight into PHP’s commenting capabilities, and explore why it’s important to pay attention to such nuances, even when you think you already know a language inside and out.
I’ll admit, this discovery was a bit embarrassing. As a developer working with PHP for several years, I had always used the standard // for single-line comments and /* */ for multi-line comments. These were the conventions I had learned early on, and I never thought much about alternatives. It was only after encountering an old PHP script during a code review that I noticed a line starting with #. At first, I thought it was just some non-standard syntax or perhaps a mistake, but after some investigation, I realized that PHP actually supports # as a comment marker, just like //.
PHP, being one of the most widely used server-side scripting languages, offers a few different ways to insert comments into code. Let’s break down the main comment syntaxes in PHP:
Single-Line Comments
Using //: This is the most commonly used syntax for single-line comments in PHP. Anything following the // symbol is ignored by the PHP interpreter until the end of the line.
// This is a single-line comment in PHP
Using #: The # symbol is another valid option for single-line comments in PHP. It works in exactly the same way as // and allows you to write a comment that ends at the end of the line.
# This is a single-line comment in PHP using #
Multi-Line Comments
Using /* */: For multi-line comments, PHP uses the /* to start the comment and */ to end it. Anything between these symbols is treated as a comment, even if it spans multiple lines.
/* This is a multi-line comment in PHP */
Nesting Multi-Line Comments: PHP allows multi-line comments to be nested within each other, which is useful in certain situations.
/* This is a multi-line comment /* This is a nested comment */ Still part of the outer comment */
So, the next time you’re working with PHP or any other programming language, remember that the small details matter. Stay curious, keep learning, and who knows what you’ll discover next.
Comments (0):