PHP Comments | PHP Tutorial - Learn with VOKS
Back

PHP Comments


What Are PHP Comments?

A comment in PHP is a piece of text inside your code that:

✅ Is ignored by the PHP interpreter

✅ Does NOT run as part of the program

✅ Is only there for humans to read

Think of comments as notes for yourself or other developers.

Simple Example

<?php
// This is a comment
echo "Hello world!";
?>

Output:

Hello world!

The comment is not displayed because PHP ignores it.

Why Comments Are Important

Comments help you:

  • Explain what your code is doing
  • Make your code easier to understand
  • Debug problems
  • Work with other developers
  • Remember your logic in the future

Without comments, code can look confusing very fast.

Types of PHP Comments

PHP has three main types.

1️⃣ Single-line Comment (//)

Used for short explanations.

// This is a single-line comment
echo "Welcome";

2️⃣ Single-line Comment (#)

Works like // but less commonly used.

# This is also a single-line comment
echo "Hello";

3️⃣ Multi-line Comment (/* */)

Used for longer notes or disabling blocks of code.

/*
This is a multi-line comment
It can span multiple lines
*/
echo "PHP is fun!";

PHP Comment Types

| Comment Type        | Symbol Used | Purpose                          | Example                     |
|---------------------|------------|----------------------------------|-----------------------------|
| Single-line comment | //         | Short explanation                | // This is a comment        |
| Single-line comment | #          | Alternative single-line comment  | # This is a comment         |
| Multi-line comment  | /* */      | Long explanations or code blocks | /* multi-line comment */    |

Example

Without Comments

<?php
$u = "Danny";
echo $u;
?>

You may forget what $u means later.

With Comments

<?php
// Store the username
$username = "Danny";

// Display the username
echo $username;
?>

Now the code is clear and readable.

Using Comments to Disable Code

This is useful for testing.

<?php
echo "This will show";

// echo "This will NOT show";
?>

Or:

<?php
/*
echo "Line 1";
echo "Line 2";
*/
echo "Only this runs";
?>

Where Comments Are Commonly Used

1. Explaining Variables

// Database connection name
$db_name = "my_database";

2. Explaining Functions

// This function calculates total price
function total($price, $qty) {
    return $price * $qty;
}

3. Section Headers

// =======================
// USER LOGIN SECTION
// =======================

Beginner vs Clean Code

❌ Bad Comment

// add 1 to i
$i = $i + 1;

This is obvious — no need to comment.

✅ Good Comment

// Increase login attempt count for security check
$login_attempts++;

This explains why, not just what.

Best Practices for PHP Comments

✔ Write comments for complex logic

✔ Keep them short and clear

✔ Update comments when code changes

✔ Explain WHY something is done

Common Mistakes

1. Too Many Comments

// create variable
$name = "John";

Not needed.

2. Outdated Comments

// This stores email
$username = "Danny";

This creates confusion.

3. Commenting Every Line

Makes code messy.

Documentation-Style Comments

Used for professional projects:

/**
 * This function registers a new user
 *
 * @param string $username
 * @param string $password
 * @return bool
 */
function registerUser($username, $password) {
    return true;
}

These are used by tools and IDEs.

Example Code:
<?php

// Start session for login system
session_start();

/*
Connect to the database
Host: localhost
User: root
Password: empty
*/
$conn = mysqli_connect("localhost", "root", "", "test_db");

// Check connection
if (!$conn) {
    die("Connection failed");
}

// Get user input from form
$username = $_POST['username'];
$password = $_POST['password'];

// Query to check user
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $sql);

// Verify user exists
if (mysqli_num_rows($result) > 0) {
    echo "Login successful";
} else {
    echo "Invalid login";
}

?>
PHP
Introduction PHP Configuration PHP CRUD (Create, Read, Update, Delete) PHP Error Handling PHP Form Submission Login System PHP Comments
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL