Developers use comments to describe the details of the code. In PHP there is three-way to describe the comments.
Shell Method
this method is used to single-line comment and it is used through # to comment the details.
<?php
# describe about code details
?>
Example:- Suppose, You created user_greetings function in the user file.
<?php
# user file
function user_greetings($name){
return 'Hello '.$name;
}
echo user_greetings('John');
?>
Single-line Comment
It is basically used for single-line comment and it works the same as the shell method. this method is copied from the c language.
Syntax:-
<?php
// describe about code details
?>
Example:- Suppose, You created user_full_name function in the user file.
<?php
// get user full name function
function user_full_name($first_name,$last_name){
return $first_name.' '.$last_name;
}
echo user_full_name('John','Taylor');
?>
Multi-line Comment
this method is used to comments for multiple lines. this method is copied from the c++ language.
Syntax:-
<?php
/*
describe
about
code details
*/
?>
Example:- Suppose, You created sum of nunbers in the user file and you defined multi-line comments for number1 and number2 .
<?php
/*
$number1=10
$number2=20;
*/
$number3=30;
$number4=40;
$sum_of_numbers=$number3+$number4;
echo $sum_of_numbers;
?>
Php Comments – Interview Questions
Q 1: What are comments in PHP?
Ans: Comments are non-executable statements used to explain code.
Q 2: Types of comments in PHP?
Ans: Single-line (//, #) and multi-line (/* */).
Q 3: Are comments executed by PHP?
Ans: No, comments are ignored by the PHP interpreter.
Q 4: Why are comments important?
Ans: They improve code readability and maintenance.
Q 5: Can comments be written inside functions?
Ans: Yes.
Php Comments – Objective Questions (MCQs)
Q1. Single-line comments in PHP start with ______.
Q2. Multi-line comments in PHP are written as ______.
Q3. Comments in PHP are used to ______.
Q4. Which comment style is NOT valid in PHP?
Q5. Multi-line comments can span ______.