HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Comments

A comment is a part of the PHP code that is **not executed** as part of the program. Its main purpose is to be read by humans — either yourself in the future or other developers working on your project.


Why Use Comments?

  • Explain logic: Describe what a complex piece of code is doing.
  • Maintenance: Help other developers (or your future self) understand the purpose of a script.
  • Debugging: Quickly "comment out" lines of code to test different parts of your program without deleting them.
  • To-do lists: Mark sections of code that still need work.

1. Single-Line Comments

For short, one-line explanations, PHP supports two styles of comments: the double-slash // (most common) and the hash # style.

<?php
    // This is a single-line comment
    echo "Hello World!";

    # This is also a single-line comment
    $x = 5; 
?>

You can also place a single-line comment at the end of a line of code:

<?php
    $sum = 10 + 20; // adding two numbers together
?>

2. Multi-Line Comments

When you need to write longer explanations or block out large sections of code, use the /* ... */ syntax. Everything between these symbols will be ignored by PHP.

<?php
    /*
      This is a multi-line comment.
      It can span across 
      multiple lines easily.
    */
    echo "Multi-line comments are great for long documentation.";
?>
Note: Multi-line comments cannot be nested. Putting a /* inside another /* */ block will cause a syntax error.

3. Using Comments for Debugging

Comments are incredibly useful when you're trying to find a bug. By commenting out lines, you can isolate the code and see exactly where the problem lies.

<?php
    $x = 5 + /* 10 + */ 5;
    echo $x; // Outputs: 10
?>
Don't Over-Comment: Good code should be relatively self-explanatory. Avoid commenting on obvious things like $x = 5; // sets x to 5. Instead, comment on the why behind the code, not the what.

Key Points to Remember

  • // and # are for single-line comments.
  • /* ... */ is for multi-line comments.
  • Comments are completely ignored by the PHP engine.
  • Comments are **not** visible in the browser's "View Source" — they stay on the server.
  • Use comments to explain complex logic or to temporarily disable code.