How to write comments in PHP ?
Last Updated :
05 Sep, 2024
Improve
Comments are non-executable lines of text in the code that are ignored by the PHP interpreter. Comments are an essential part of any programming language. It help developers to understand the code, provide explanations, and make the codebase more maintainable.
Types of Comments in PHP
PHP supports two main types of comments: single-line and multi-line comments. Each type has its specific syntax and use cases.
Single-Line Comments in PHP
Single-line comments are used for brief explanations or notes that fit on a single line. The single line commments begin with either // or #.
Syntax
// Single-line comment using double slashes
or
# Single-line comment using hash symbol
Example
<?php
// Define a variable with some values
$var = "Hello GeeksforGeeks";
# Output "Hello GeeksforGeeks"
echo $var;
?>
Output
Hello GeeksforGeeks
Multi-Line Comments
Multi-line comments, also known as block comments, are used for longer explanations or when you need to comment out multiple lines of code. They begin with /* and end with */.
Syntax
/*
This is a multi-line comment.
It contains multiple lines.
*/
Example
<?php
/*
Define a variable with $ symbol
and assign some values. Use echo
to print the value of variable
*/
$var = "Hello GeeksforGeeks";
echo $var;
?>
Output
Hello GeeksforGeeks