How to Write Single and Multi-Line Comments in PHP

Comments in PHP are non-executable lines of code used to document your script, explain logic, or temporarily disable code blocks during testing. This article explains how to write both single-line and multi-line comments in PHP using clear, practical examples.

Single-Line Comments

PHP supports two different syntaxes for writing single-line comments. Everything on the line to the right of the comment symbol is ignored by the PHP interpreter.

1. Using Double Slashes (//)

This is the most common method for creating a single-line comment.

<?php
// This is a single-line comment
echo "Hello, World!"; // This is an inline comment
?>

2. Using the Hash Symbol (#)

This syntax, often called Unix shell-style, is also fully supported in PHP.

<?php
# This is also a single-line comment
echo "Hello, World!";
?>

Multi-Line Comments

When you need to write longer explanations or temporarily disable larger blocks of code, you should use multi-line comments.

Using Slash and Asterisk (/* and */)

Multi-line comments start with /* and end with */. Anything placed between these two markers will not be executed.

<?php
/*
This is a multi-line comment in PHP.
You can use it to span across multiple lines
to explain complex logic or functions.
*/
echo "Hello, World!";
?>

Note: Avoid nesting multi-line comments inside one another, as this will result in a syntax error.