🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

PHP – Comments

Learn all three PHP comment styles: single-line, multi-line, and DocBlock. Discover when and why to comment your code for clarity.

PHP Comments

A comment is a line of text situated deep within your programming code that is completely and entirely ignored by the PHP engine. When the server processes your script, it skips over comments exactly as if they did not exist.

Why Use Comments?

  1. Code Documentation: Leaving clear explanations allows other developers (or future you) to instantly understand complex database queries and algorithms.
  2. Debugging: Instead of deleting a stubborn block of code causing syntax errors, you can simply “comment it out” safely while you hunt down the issue.
  3. Reminders: Tagging incomplete sections of your script using // TODO: makes tracking project milestones substantially easier.

There are two main types of PHP comments:

1. Single-line Comments

A single-line comment operates exactly how it sounds. Everything proceeding the symbol on that specific line is completely ignored. PHP natively supports both the // and # symbols.

<?php
  // This is a C++ style single-line comment
  echo "Welcome to my website!";
  
  # This is a Unix Shell-style single-line comment
  echo "<p>My name is CodesCompiler.</p>";

  echo "Server status OK"; // We can place comments AFTER valid code!
?>

2. Multi-line Comments

When you need to write long paragraphs of documentation or disable huge chunky blocks of code, pressing // on every single line becomes tedious. Instead, you can encase massive sections seamlessly inside a multi-line comment block: /* ... */.

<?php
  /* 
  This is a huge multi-line comment block
  that spans over multiple lines. It is heavily
  utilised to describe advanced functions.
  */
  
  echo "This code is executed.";

  /* 
  echo "This code is effectively hidden!";
  echo "The server won't execute this!";
  */
?>

Internal Line Commenting

Because multi-line comments utilize distinct opening and closing tags, you can cleverly drop them directly into the total center of an active line of code!

<?php
  // Removing '15' momentarily without deleting it
  $x = 5 /* + 15 */ + 5;
  
  echo $x; // Outputs: 10
?>

Interactive Example

Test out commenting in the live interface below. Try deleting the // symbols in front of the second echo command to reactivate it!

Preview