HOME HTML EDITOR C JAVA PHP

PHP Comments

Comments in PHP are parts of the code that are not executed by the server. They are used to explain what the code does, making it easier for others (and yourself) to understand.

1. Single-Line Comments

Use these for short notes or to temporarily disable a single line of code. You can use // or #.

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

  # This is also a single-line comment using a hash
  echo "Learning PHP!";
?>

2. Multi-Line Comments

Use these when your explanation requires more than one line or to "comment out" large blocks of code.

<?php
  /*
    This comment block
    spans across multiple
    lines of code
  */
  echo "Multi-line comments are great for documentation.";
?>

3. Comments Inside Code

You can even use comments to ignore parts of a code line, though this is less common:

<?php
  $x = 5 /* + 15 */ + 5;
  echo $x; // Outputs 10
?>
Why use comments?
* To let others understand your logic.
* To remind yourself what a complex function does.
* To prevent code from running while testing new features.