Introduction
When writing PHP code, not everything is meant for the computer. Sometimes, you need to leave notes for yourself or other developers — and that’s where comments come in.
Comments are ignored by PHP during execution, but they play a very important role in writing clean and understandable code.
In this tutorial, you will learn what PHP comments are, their types, and how to use them effectively.
What You Will Learn
- What comments are in PHP
- Why comments are important
- Types of PHP comments
- How to use comments in real code
- Best practices for writing comments
What are Comments in PHP?
Comments are lines of text that PHP ignores while executing the code.
They are used to:
- Explain what the code does
- Make code easier to understand
- Temporarily disable code during testing
Comments are for humans, not for PHP.
Why Are Comments Important?
Comments improve code quality and readability.
They help you:
- Understand code logic easily
- Make your code readable for others
- Revisit your code after a long time
- Debug and test your code
Good code + good comments = professional coding 💡
Types of Comments in PHP
PHP supports three types of comments.
Single-Line Comment Using //
This is the most commonly used comment style.
<?php
// This is a single-line comment
echo "Hello World";
?>
Anything written after // on that line is ignored by PHP.
Single-Line Comment Using #
This also creates a single-line comment, but it is less commonly used.
<?php
# This is also a single-line comment
echo "Welcome to PHP";
?>
👉 This style is less commonly used but may appear in older code.
Multi-Line Comment Using /* */
Used when you want to write comments in multiple lines.
<?php
/*
This is a multi-line comment.
It can span across multiple lines.
Useful for long explanations.
*/
echo "Learning PHP";
?>
Using Comments to Disable Code
Comments are often used to temporarily stop code execution.
<?php
// echo "This will not be displayed";
echo "This will be displayed";
?>
👉 This is very helpful while testing or debugging.
Real-World Example
<?php
// Set default user role
$role = "guest";/*
Check if user is admin
and grant full access
*/
if ($role === "admin") {
echo "Full Access";
} else {
echo "Limited Access";
}
?>
👉 Comments help explain why logic is written, not just what it does.
Best Practices for PHP Comments
- Write comments to explain why, not obvious things
- Avoid too many unnecessary comments
- Keep comments short and clear
- Update comments when code changes
❌ Bad comment:
$x = 10; // assign 10
✅ Good comment:
$x = 10; // default user age
Common Mistakes to Avoid
- Writing obvious comments
- Leaving outdated comments
- Overusing comments instead of clean code
- Writing unclear or confusing comments
When Should You Use Comments?
Use comments when:
- Logic is complex
- Code is not self-explanatory
- You are working in a team
- You need to debug code
👉 Avoid comments when code is already very clear.
Comments vs Clean Code
While comments are helpful, writing clean and readable code is even more important.
👉 Example:
$userAge = 18; // good variable name (no need for extra comment)
Instead of:
$u = 18; // user age
👉 Clean code reduces the need for too many comments.
Difference Between Comments and Echo
| Feature | Comments | Echo |
|---|---|---|
| Visible in browser | ❌ No | ✅ Yes |
| Executed by PHP | ❌ No | ✅ Yes |
| Purpose | Explanation | Output |
FAQs
No, comments are ignored by PHP during execution.
Use // for single-line comments and /* */ for multi-line comments.
Yes, you can temporarily disable code using comments.
# comment still used in PHP? Yes, but it is less common compared to //.
No, PHP comments are not visible because they are removed before sending output to the browser.
📝 Practice Tasks
✅ Task 1
Write a PHP file and add:
- One
//comment - One
#comment - One
/* */multi-line comment
✅ Task 2
Comment out an echo statement and see what happens in the browser.
Summary
- Comments are ignored by PHP
- They help improve code readability
- PHP supports three types of comments
- Use comments wisely and keep them meaningful
- Avoid unnecessary or outdated comments
Learning Outcome
After this lesson, you should be able to:
- Understand what comments are
- Use all three types of PHP comments
- Read PHP code more confidently
