PHP Comments


Comments are an essential part of any programming language, including PHP. They are notes within your code that help you and others understand what the code does, but they don’t affect how the script runs. Proper use of comments makes your code easier to read, maintain, and debug.

What Are Comments in PHP?

Comments are ignored by the PHP engine during execution. They serve as documentation for:

  • Explaining the purpose of code

  • Adding reminders or TODOs

  • Temporarily disabling parts of the code for testing

Types of Comments in PHP

PHP supports three types of comments: single-line, hash-style, and multi-line comments.

Single-Line Comments

Single-line comments are written using //. Everything after // on that line is ignored by PHP.

<?php
// This is a single-line comment
echo "Hello, World!"; // This prints a message
?>
  • The first line is a comment by itself

  • You can also add a comment after a statement

Hash (#) Comments

Another way to write single-line comments is by using #. It works the same as // but is less commonly used.

<?php
# This is a hash-style comment
echo "Hello PHP!"; # Prints the message
?>
  • Functionally identical to //

  • Mostly used in scripts coming from Linux or shell environments

Multi-Line Comments

Multi-line comments allow you to write longer explanations that span several lines. They start with /* and end with */.

<?php
/*
   This is a multi-line comment.
   You can explain complex logic here
   or temporarily disable multiple lines of code.
*/
echo "PHP Comments Example";
?>
  • Useful for explaining functions, loops, or sections of code

  • Can also be used to temporarily “comment out” code during testing

Why Use Comments?

  1. Code readability: Makes your code understandable for others and for future you.

  2. Debugging: Helps identify sections of code or temporarily disable code without deleting it.

  3. Documentation: Acts as internal documentation for anyone who works on your code.

Example:

<?php
// Define user information
$username = "Vicky";
$age = 25; // User age in years

/*
   Display a welcome message
   using the variables defined above
*/
echo "Welcome, " . $username . "! Your age is " . $age;
?>

Here, comments explain each section of the code for clarity.

Best Practices for PHP Comments

  • Be concise: Don’t write overly long comments that repeat what the code already says.

  • Explain why, not just what: Focus on why something is done rather than stating the obvious.

  • Keep comments up-to-date: Outdated comments can confuse readers.

  • Use multi-line comments for blocks: Especially for functions or complex logic.

  • Use single-line comments for simple explanations: For small statements or inline notes.

Commenting Out Code

Sometimes you want to disable code temporarily for testing or debugging. Comments allow you to do this without deleting code.

<?php
// This code is temporarily disabled
// echo "This line will not run";

// Multi-line example
/*
$a = 5;
$b = 10;
echo $a + $b;
*/
?>
  • The server ignores all commented lines

  • You can quickly restore the code by removing the comment symbols

Comments Inside HTML

You can also add comments in HTML when embedding PHP. These comments are visible in the HTML source, unlike PHP comments which are hidden from the browser.

<!DOCTYPE html>
<html>
<head>
  <title>PHP Comments Example</title>
</head>
<body>
  <!-- This is an HTML comment -->
  <p>
    <?php
    // PHP comment inside PHP block
    echo "Hello from PHP!";
    ?>
  </p>
</body>
</html>
  • PHP comments (//, #, /* */) are not visible in the browser source

  • HTML comments (<!-- -->) are visible in the source code

Using Comments for Functions

Comments are extremely useful when writing functions or reusable code blocks. You can describe what the function does, its parameters, and the expected output.

<?php
/*
   Function: greetUser
   Purpose: Displays a greeting message for a user
   Parameters: $name (string) - Name of the user
   Returns: None
*/
function greetUser($name) {
    echo "Hello, " . $name . "!";
}

// Call the function
greetUser("Vicky");
?>
  • Multi-line comments act as mini-documentation

  • Helps anyone using your code understand its purpose

Summary of the Tutorial

PHP comments are a small but essential part of programming. They:

  • Improve readability and maintainability

  • Allow you to temporarily disable code

  • Help document your code for yourself and others

By using single-line, hash, and multi-line comments properly, your PHP scripts will be easier to understand, debug, and maintain. Comments don’t affect the execution but can make a huge difference in long-term code quality.


Practice Questions

  1. Write a PHP script that prints “Learning PHP Comments!” using echo and include a single-line comment explaining what the script does.

  2. Create a PHP file where you define a variable $name = "Vicky" and $age = 25, then print them in a sentence. Add comments describing each variable and the output line.

  3. Write a PHP script using a multi-line comment to explain the purpose of the script, then define two numbers and print their sum.

  4. Create a PHP script that temporarily disables a line of code using // and prints “This line runs.” Add comments explaining why the other line is disabled.

  5. Write a PHP file using # style comments to describe a variable $city = "Delhi" and print it using echo. Include comments on each step.

  6. Design a PHP script that contains two PHP blocks: the first prints “First block active” and the second prints “Second block active.” Add comments in both blocks.

  7. Write a PHP page with HTML and PHP embedded. Include an HTML comment and a PHP comment explaining what each section does. Print a dynamic greeting using PHP.

  8. Create a function greetUser($name) that prints a greeting. Add a multi-line comment above the function explaining its purpose, parameters, and output.

  9. Write a PHP script that uses comments to temporarily disable multiple lines of code including variable assignments and echo statements. Then restore them by removing comments.

  10. Create a PHP page where you demonstrate all three types of comments (//, #, /* */) in one script and print “Comments Example.” Include inline comments explaining each comment type.


Go Back Top