EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Comments in PHP

Home » Software Development » Software Development Tutorials » PHP Tutorial » Comments in PHP

Comments in PHP

Introduction to Comments in PHP

The word comment itself express its meaning as commenting out something. If we comment on anything in the PHP program file, it will not be compiled with the code. The compiler or the interpreter will simply ignore this. There are various ways we can go with commenting on anything in the PHP programming language. PHP has a single line and multiple line comment options as well. Also, in the single-line comment, we can use the / or # as per our convenience. We should always write the appropriate language in the comment so that anyone who is reviewing that code can read and understand that comment.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Again, we can use the comment in the below-mentioned ways in PHP.

Single Line Comment:

// This is a single-line comment
# This is a single-line comment
/*
This is a multiple line comment – line no – 1
This is a multiple line comment – line no – 2
… and so on …
*/
# this is the single-line comment
; this is a PHP.INI style commenting

How does Comments in PHP work?

The moment we write the comment in the code it will be lighter in view comparatively as compared to the actual code. If we have written code and the comment in the same file, in this case, the comment will not be executed while running that PHP file. So let’s see the same with an example.

<?PHP
echo "This is my first PHP Program";
// this is the first program
?>

This will give only ‘This is my first PHP Program’ as an output, not the comment part.

Why we should use comments?

We should use the comment in between the PHP program code to make sure we had enough help comments so that one can easily read and understand the code. In comment, we can also for the purpose of writing the segment of the code, change the date, modification date, modified by, etc. It is highly recommended for a developer to use the comment in the programming code so that things can be understood easily with little or no effort whenever required.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Examples

Now, it’s time to see some quick example the check the behavior of the comment in the program.

Example #1

<?php
echo "This is my first PHP Program";
// this is the first program
?>

Output 

Check the behavior

Example #2

Single line comment in the program

Code:

<?php
echo date_default_timezone_get(); //to get the timezone
echo "<br>"; //line brea;
date_default_timezone_set('Asia/Kolkata'); // setting the timezone to Asia/Kolkata
echo date_default_timezone_get(); //to get the timezone
?>

Output 

comments in php

Example #3

Multi-line comment in the program

Code:

<?php
/*
This is a function that will take time in minutes and will return that in hours and minutes
We use will this function further to test with some examples.
*/
function hoursandmins($minutes, $format = '%01d Hours, %02d Minutes')
{
if ($minutes < 1) {
return;
}else if($minutes<10){
if($minutes==1){
return '0'.$minutes.' Minute';
}
return '0'.$minutes.' Minutes';
}else if($minutes<50){
return $minutes.' Minutes';
}
$hours = floor($minutes / 60);
$minutes = ($minutes % 60);
$format = "";
if($hours>0){
if($hours>1){
$format .= '%01d Hours';
}else{
$format .= '%01d Hour';
}
}
if($hours>0 && $minutes>0){
$format .= ', ';
}
if($minutes>0){
if($minutes>1){
$format .= ' %02d Minutes';
}else{
$format .= ' %01d Minute';
}
}
return sprintf($format, $hours, $minutes);
}
// time to check the time in descriptive form
echo hoursandmins(20);
echo "<br>";
echo hoursandmins(120);
?>

Output 

Time in Minutes and Hours

Any comments written before any function about that function is commonly known as the function documentation. So, in the given scenarios we can say this comment as a function document. This is really nice to use the comment like that. To make the PHP program more robust we have various PHP standards that say we should not use the comment inside the function as we can do the same in the function documentation.

Example #4

Mixed comment

In a PHP program file, we can combine both PHP and HTML. Considering this, we will use the HTML commenting method to comment out the HTML part whenever required, and the PHP comments can be used for the PHP related code as per the business requirements. We can use any kind of PHP comments and the HTML comments in a PHP-HTML file.

Code:

<html>
<body>
<h1>PHP with HTML</h1>
<!-- this is html comment -->
<?php
echo "Hello World!"; // this is PHP comment
?>
</body>
</html>

Output 

PHP With HTML

Example #5

Single line comment using #

We can use the single line commenting using the # as well. Let’s see an example of the code for the same. We can see this line of commenting is not that much popular. Very few developers use these kinds of comments. This is really recommended for the while commenting for the function documentation or the file level documentation.

Code:

<?php
# $i=10;
# $j=20;
# echo $i + $j;
echo "Hello World!";
# this is PHP comment
?>

Output

Single line using #

Conclusion

A comment is really a call of time in the code. We can use the comment in our PHP program file. This will help us to make the code more understandable. We should use the comment before starting a function or the class as most programming languages have their own code standard. Usually, the comment before any function or the class called the documentation of that function or the class.

Recommended Articles

This is a guide to Comments in PHP. Here we discuss the introduction, How does it work, why we should use comments along with Examples and outputs. You can also go through our other suggested articles to learn more –

  1. PHP Split Array
  2. PHP GET Method
  3. Inheritance in PHP
  4. PHP Encryption

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you