Comments in PHP
When writing PHP, not every line has to “do” something — sometimes you just want to leave notes for yourself or others who’ll read your code later. These notes are called comments, and PHP ignores them when running your script.
Comments are super useful for explaining what a piece of code does, or even for temporarily disabling parts of your code when testing.
Single-line comments
You can write single-line comments in two ways:
// This is a single-line comment
# This works too!
Everything after // or # on that line will be ignored by PHP. For example:
// Output a welcome message
echo "Welcome Home!"; // Another way to add a quick note
Multi-line comments
If you need to write longer explanations, use the /* ... */ format:
/*
This is a multi-line comment.
PHP will ignore everything until it finds the closing tag.
*/
Ignoring Code
Sometimes you just want to “turn off” a line without deleting it:
// echo "This line won’t run!";
That’s especially helpful when debugging — you can comment out code to see what happens without losing it.
Quick tip: Good comments explain why your code exists, not just what it does. The code itself should already show what — use comments to add clarity or context!