Beginner Fundamentals

PHP Comments

Comments are notes in your code that PHP ignores when running the script. They explain what the code does and help other developers understand it.

Single-line comments

Use // or # to comment a single line. Everything after the symbol is ignored.

<?php
// This is a comment
echo "Hello"; // comment after code

# This also works as a comment
echo "World";

Multi-line comments

Use /* to start and */ to end a comment that spans several lines.

<?php
/*
This is a longer comment.
It can cover multiple lines.
Useful for detailed explanations.
*/
echo "Comments are helpful";

Why use comments

  • Explain complex logic
  • Leave reminders or to-do notes
  • Temporarily disable a line of code
<?php
// echo "This line will not run";
echo "Only this line runs";

Use comments to clarify, not to repeat the obvious.