What is the syntax for commenting in PHP?
Types of Comments in PHP
PHP has three main types of comments:
- Single-line comments (two styles)
- Multi-line comments
1. Single-Line Comments
Single-line comments are used for short explanations or to disable a single line of code. There are two syntaxes:
Style A: Double Slash //
This is the most common style for single-line comments.
Syntax:
// This is a comment
echo "Hello World"; // This is an inline comment
Example:
<?php
// Set the user's favorite color
$favoriteColor = "blue";
echo "Your favorite color is " . $favoriteColor; // Output the color to the screen
?>
Style B: Hash/Pound Symbol #
This works exactly the same as // but is less commonly used.
Syntax:
# This is also a single-line comment
echo "Hello World"; # This is an inline comment
Example:
<?php
# Get the current timestamp
$currentTime = time();
echo "The current timestamp is: " . $currentTime; # Display the timestamp
?>
Note: Both // and # are functionally identical. The choice between them is a matter of personal or team preference.
2. Multi-Line Comments (Block Comments)
Multi-line comments are used for longer explanations, commenting out large blocks of code, or for formal documentation headers (like for functions or classes).
Syntax:
/*
This is a multi-line comment.
It can span across several lines.
Everything between the slash-asterisk and asterisk-slash is ignored by the PHP interpreter.
*/
Example 1: Documenting a Function
<?php
/*
* calculateTotalPrice
* Calculates the total price including tax
*
* @param float $subtotal The price before tax
* @param float $taxRate The tax rate as a decimal (e.g., 0.08 for 8%)
* @return float The total price after tax
*/
function calculateTotalPrice($subtotal, $taxRate) {
$taxAmount = $subtotal * $taxRate;
$total = $subtotal + $taxAmount;
return $total;
}
?>
Example 2: Commenting Out a Block of Code for Debugging
<?php
echo "This line will execute.";
/*
echo "This code is temporarily disabled.";
$debugValue = 42;
var_dump($debugValue); // This function won't run either
*/
echo "This line will also execute.";
?>
Practical Examples & Best Practices
Example: Mixed Comments in a Script
<?php
# config.php
// This file handles basic configuration settings for the application.
/*
* Database Configuration Constants
* These constants are used throughout the app to connect to the MySQL database.
*/
define('DB_HOST', 'localhost'); // Database server address
define('DB_USER', 'app_user'); // Username for the database
define('DB_PASS', 'secret_password'); # Password for the database user
define('DB_NAME', 'my_app_db'); // Name of the database to use
// Establish database connection
$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check if connection failed
if ($connection->connect_error) {
/*
* In a production environment, we would log this error
* instead of showing it directly to the user.
*/
die("Connection failed: " . $connection->connect_error);
} else {
echo "Database connected successfully!"; // Success message
}
?>
Best Practices for Effective Commenting
-
Explain WHY, not WHAT: Good comments explain the purpose or reasoning behind the code, not just what it does. The code itself should be clear enough to show "what."
Bad:
$i++; // Increment i by oneGood:
$i++; // Move to the next user in the array -
Keep Comments Updated: When you change code, remember to update the related comments. Outdated comments are worse than no comments.
-
Use Multi-line for Documentation: Use
/* */for function/class descriptions and//or#for inline explanations. -
Avoid Obvious Comments: Don't state the obvious. Your code should be self-explanatory when possible.
Summary Table
| Comment Type | Syntax | Use Case |
|---|---|---|
| Single-line | // Comment here |
Short explanations, inline comments |
| Single-line | # Comment here |
Alternative style for single-line comments |
| Multi-line | /* Comment here */ |
Long descriptions, documentation blocks, commenting out code sections |
Key Points to Remember
- Comments are ignored by the PHP interpreter and have no effect on performance.
- They are essential for collaboration and for your future self to understand why you wrote code a certain way.
- Choose a consistent style (
//vs#) for your project and stick with it. - Well-commented code is a hallmark of a professional developer!
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
