PHP Comments
In PHP, comments are used to explain code and make it more readable. They are ignored by the PHP interpreter and do not affect the execution of the script. There are three types of comments in PHP:
-
Single-line Comments:
-
Using
//
: Everything following//
on that line is treated as a comment.// This is a single-line comment echo "Hello, World!"; // This is also a comment
-
Using
#
: Everything following#
on that line is treated as a comment.# This is a single-line comment echo "Hello, World!"; # This is also a comment
-
-
Multi-line Comments:
-
Using
/* ... */
: Everything between/*
and*/
is treated as a comment. This can span multiple lines./* This is a multi-line comment. It can span multiple lines. */ echo "Hello, World!";
-
Using
/** ... */
: This is used for PHPDoc comments and is often used for documenting code./** * This is a PHPDoc comment. * It is used for documenting code. */ echo "Hello, World!";
-
Examples and Output
Here’s an example script with all types of comments:
<?php
// This is a single-line comment
echo "Hello, World!"; // This is an inline comment
# This is another single-line comment
echo "Hello again!";
/*
This is a multi-line comment.
It spans several lines.
*/
echo "Goodbye, World!";
?>
Output:
Hello, World!
Hello again!
Goodbye, World!
Comments are essential for documenting code, making it easier for others (and yourself) to understand what the code does and why certain decisions were made.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.