0
0
PHPprogramming~5 mins

Comments in PHP

Choose your learning style9 modes available
Introduction

Comments help you write notes inside your PHP code. They explain what the code does without affecting how it runs.

To explain tricky parts of your PHP code for yourself or others.
To temporarily stop some code from running while testing.
To add reminders or to-do notes inside your code.
To describe the purpose of a function or variable.
To make your code easier to understand later.
Syntax
PHP
// This is a single-line comment
# This is also a single-line comment
/* This is a
   multi-line comment */
Single-line comments start with // or # and end at the line's end.
Multi-line comments start with /* and end with */. They can span many lines.
Examples
This shows a single-line comment before a print statement.
PHP
<?php
// This is a single-line comment
echo "Hello!";
?>
You can also use # for single-line comments, even after code on the same line.
PHP
<?php
# Another single-line comment
$x = 5; # Comment after code
?>
This example uses a multi-line comment to explain something over several lines.
PHP
<?php
/* This is a
multi-line comment
that spans three lines */
echo "Done";
?>
Sample Program

This program uses comments to explain each step: setting a name and printing a greeting.

PHP
<?php
// This program prints a greeting

/* Define a variable with a name */
$name = "Alice";

// Print the greeting message
echo "Hello, " . $name . "!";
?>
OutputSuccess
Important Notes

Comments do not run or affect your program's output.

Use comments to make your code easier to read and maintain.

Too many comments can clutter code; write clear code and comment only when needed.

Summary

Comments explain your PHP code without running.

Use //, # for single-line and /* ... */ for multi-line comments.

Comments help you and others understand your code better.