Skip to main content

PHP Comment Style Guide and Coding Standards

Welcome to PHPBash. In this PHP Tutorial, we will learn about Basics PHP Comments. In our Previous Tutorial, we learn about basics of PHP syntax as well fundamentals of comments. Basically, PHP Comments are part of PHP Program which is not executed by PHP  parser while Executing PHP Program. PHP Comments can be Single line or multiline. 

PHP Comment Style Guide and PHP Coding Standards

In this, PHP tutorial covers all the topics of Basics of PHP Comments styles and Coding Standard like
1. What are Comments in PHP Programming Language? 
2. Need of PHP Comments
3. Types of PHP Comments
4. PHP Comment Styling
5. PHP Comments Standards with Best Practices
6.PHP Comments Example with Valid and Invalid Examples
Now let's start  this tutorial with understanding What are Comments in PHP Programming Language?

What are Comments in PHP Programming Language? 

In PHP, Comments are part of PHP program or script which is not Interpreted by PHP Parser while Executing PHP Script or PHP program. In any programming language, Comments are part of code which never Executed language compiler or Interpreter. 
Comments describe the code functionality and how to use it.  Comments are also useful for providing more details about particular statement or function or any other statement. 

Need of PHP Comments

1. The main purpose of PHP Comments are to describe the PHP program.
2. It helps of Comments, Other people can understand code.
3. The readability of Script or program can be improved with Comments.
4. The particular Logic or part of code can be described with help of comments.
5. Information other than PHP code, can be included within file like License Information,Author details, Version of file etc. For Example:

PHP Comments Example
PHP Comments Example

 Types of PHP Comments

Like other programming language PHP also supports two types of Comments- Single Line Comments and Multi-line Comments. Single Line Comments are useful for shot description where as multi-Line Comments are useful for long description. 

PHP Single Line Comments

For creating Single Line Comments in PHP Code, you need to use // or # before the comment line. Consider following example, 
<?php
    # Single Line Comment 1
    # Single Line Comment 2
    # Single Line Comment 3
    // Another way to use Single Line Comments in PHP
    echo "Example of Single Line Comment.";
?>
PHP Comments are also useful for skipping certain part of code. For Example:
<?php
    //You can also use comments to leave out parts of a code line
    $ctr = 85 /* - 52 */ * 54;
    echo "The Counter value".$ctr;
?>

PHP Multi-Line Comments

For creating Multi-line Comments in PHP Code, you need to use /* ... */Consider following example, 
<?php
    /*
       This is Multiline Comment Example
       Where more than one lines are available
       in Comments.
    */
    echo "Example of Multi-Line Comment.";
?>

PHP Comment Styling Guide

The Following Example demonstrate various Styles of PHP Comments used in PHP Program for describing details.
<?php

//=================================================================
// COMMENTS IN LARGER FONT
//=================================================================

//-----------------------------------------------------
// Comments in Smaller font
//-----------------------------------------------------

/* Starting of Program */

# Flag 1
# Flag 2
# Flag 3

/*
* This is Multi line Comment 
* with more a detailed explanation
*/

// Single Line Comment 1

/* Ending of Program */

?>
PHP Comments Styles Example
PHP Comments Styles Example

PHP Comments Standards with Best Practices

1. Single-line comments MUST use two forward slashes
<?php
// Comment 1
echo "Single-line comments MUST use two forward slashes";
?>

2. Multi-line comments MUST use the block format
<?php
/**  
Comment 1 
*/
echo "Multi-line comments MUST use the block format";
?>

3. Header comments SHOULD use the block format
<?php
/** 
* Name of code section 
*/
echo "Header comments SHOULD use the block format";
?>

4. Comments MUST be on their own line
<?php
//Name of code section 
echo "Comments MUST be on their own line";
?>

5. Blocks of code SHOULD be explained or summarized
<?php
//The details of logic should be given
echo "Blocks of code SHOULD be explained or summarized";
?>

7. Ambiguous numbers MUST be clarified

8. External variables MUST be clarified
<?php
//$conn is Connection variable for database
$conn=mysqli_connect("localhost","test","test","test_db");
?>

PHP Comments Example with Valid and Invalid Examples

The following Example Demonstrates the valid and invalid examples of various comments in PHP.

1. Single-line Comments

Single-line comments MUST use two forward slashes.

Invalid

<?php
/* This is a comment */
// End of File
?>
Reason: Incorrect because it uses /* and */ for a single-line comment.

Valid

<?php
// This is a comment
// EOF
?>

2. Multi-line Comments

Multi-line comments MUST use the block format.

Invalid

<?php
// This is a
// multi-line
// comment

// EOF
?>
Reason:  Incorrect because it uses // for a multi-line comment.

Valid

<?php
/**
 * This is a
 * multi-line
 * comment
 */

// EOF

?>

3. Divider Comments

Divider comments SHOULD use the block format with 75 asterisks in between.

Invalid

<?php
/**#######################################################################*/

// EOF
?>
Reason:  Incorrect because it uses # instead of *.

Invalid

<?php
/******************/

// EOF
?>
Reason:  Incorrect because it uses 15 instead of 75 *.

Valid

<?php
/**
 * Beginning + Middle + End
 * 3 spaces + 75 spaces + 2 spaces = 80 character line limit
 */

/******************************************************************************/

// EOF
?>

4. Comments

Comment MUST be on their own line.

Invalid

<?php
test_function(); // Calling test function

// EOF
?>
Reason:   Incorrect because // Prints welcome message is not on its own line.

Valid

<?php
// Calling test function
test_function(); 
// EOF
?>

5. Blocks of Code

Blocks of code SHOULD be explained or summarized.

Invalid

<?php
if ($expression2
        {
// Comment 1
} else if($expression3){
// Comment 2
}
if ($expression4) {
// Comment 3
} elseif ($expression5) {
// Comment 4
} else {
// Comment 5
}
// Comment 6

// EOF
?>
Reason:   Allowed, but block of code should be explained or summarized.

Valid

<?php
/**
 * Line 1 description 
 * Line 2 description 
 * Line 3 description 
 */

if ($expression2
        {
// Comment 1
} else if($expression3){
// Comment 2
}
if ($expression4) {
// Comment 3
} elseif ($expression5) {
// Comment 4
} else {
// Comment 5
}
// Comment 6


// EOF
?>
I would acknowledge you sharing your individual experiences about "PHP Comment Style Guide and Coding Standards". If you have any doubt/query related to PHP or Web Technologies, Please let me know in Comment Section. 
I will certainly take care that, I should give reply for every single comment on my blog posts. So, let’s fire the discussion up! See you in the comments section below..

 Sharing is caring ❤️

Thank you for Visiting PHPBashKeep Visiting for more Interesting concepts of Php language and web technologies from basics to advanced. As well we will also share some ready-to-use, useful code snippets for beginners to kick start their web development project. 

Do you want to build a modern, lightweight, responsive website and launch quickly? 

 I help to create the best websites / web applications and will be available for freelance work. Let's Get in touch: phpgems28@gmail.com

Comments

You may also like

PHP Constants

W elcome to PHPBash. In this PHP Tutorial, we will learn about Basics of PHP Constants.  In our Previous Tutorial , we learn about different concepts related to PHP variables. A constant is a name or an identifier with a fixed or permanent value. In Programming, Both constants and variables are used for storing data or values and act as container. Constants are more like variables with one difference that, Constants can not be modified/Changed during execution of program.  PHP Constants In this, PHP tutorial covers all the topics of Basics of PHP Constants  like         1.  PHP Constants         2.  Using the PHP define() method.         3.  Using the PHP const keyword.         4.  PHP 7 Constant Arrays         5.  PHP Global Constants         6.  PHP constant() Function          7. PHP Class constant array Now let's start  this tutorial with understanding P HP Constants and their use .  PHP Constants A constant is an identifier whose value cannot be changed or modified during the

Basics of Php Syntax

Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, we will learn details about Precious Tips To Help You Get Better At Basics of PHP Syntax, Including PHP script in HTML and Executing PHP Script. As well  PHP case sensitivity, PHP tags and their types ,  Comments in PHP,  Whitespace in PHP &  PHP Block Concept. Basics of Php Syntax This PHP tutorial covers all the topics of Basics of PHP Syntax like           1. Basic PHP Syntax         2. PHP Case Sensitivity         3. Types of PHP Tags         4. Comments in PHP         5. White Spaces in PHP          6. Blocks in PHP          7. PHP Expressions and Statements          8. Executing/Running PHP Script in Windows Command Prompt(cmd) Now let's start  this tutorial with understanding Basic PHP Syntax. Basic PHP Syntax   PHP is very popular and most widely used and the reason is that,PHP is  simple to learn and use.   The code of PHP is executed on server

PHP 7 Data Types: Scalar, Compound and Special

W elcome to PHPBash. In this PHP Tutorial, we will learn about Basics of PHP Data Types.  In our  Previous Tutorial , we learn about different concepts related to PHP variables.  Data types define what kind of data or values a variable will hold. With help of data types, computer can understand the type of data stored in variable and as per its type, memory will be allocated. so data types are very important and crucial concept in any programming language.  In PHP 7, there are total 10 different data type s. They are divided in to Scalar Types, Compound Types and Special Types. As PHP is loosely typed, there is no need to specify data type while declaring variables. PHP Handles typecasting or PHP Juggling automatically. You can read more about it, in this tutorial. In this, PHP tutorial covers all the topics of Basics of  PHP 7 Data Types  like 1.  PHP 7 Data Types and their Sub Types Now let's start  this tutorial with understanding  PHP 7 Data Types and  their  Sub Types. PHP 7

PHP Programming Tutorials for Beginners | PHP Variables Types

Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, we will learn about PHP Variables, PHP Variables Type s,  PHP Global Variables,  php variable scope,  static variable in php.  As well  Rules for Variable declaration. PHP Programming Tutorials for Beginners | PHP Variables Types This PHP tutorial covers all the topics of Basics of PHP Variable like         1.  PHP Variables         2.  Rules for Variable declaration         3. Creating (Declaring) PHP Variable and Assigning value         4. PHP Variable Scope & Lifetime         5. PHP Static Variables         6. Type Casting and Type Juggling in PHP         7.  PHP Variable Case Sensitiveness          8. Destroying PHP Variables using unset()          9. PHP Programming Examples/Scripts on PHP Variable concept Now let's start  this tutorial with understanding PHP Variables. PHP Variables In computer programming, Variables are act as containers for stor

PHP Programming Fundamentals - Introduction to PHP

Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, I will provides in-depth knowledge of Introduction of PHP scripting language.  PHP Programming Fundamentals - Introduction to PHP  This PHP tutorial covers all the topics of PHP Introduction such as          1. What is PHP?         2. Basic Uses of PHP         3. Characteristics of PHP         4. Prerequisite to Start with PHP         5. What PHP File can contain?         6. Syntax of PHP Code          7.  What you will need to Work with PHP?  Let's start PHP tutorial with basic question, What is PHP? What is PHP? PHP Stands for PHP: hypertext Preprocessor. PHP is Open source general-purpose High Level  object-oriented  Scripting Language. It is used for web application development and can also be embedded into hypertext markup language (HTML).  Many Web site like Yahoo, Wikipedia,  Facebook,  Tumblr,  Wordpress,  MailChimp,  Flickr. etc, are developed usin

PHP echo and print Statements | PHP Echo Vs Print

Welcome to PHPBash. In this PHP Tutorial for Beginners , we will learn about PHP echo and print Statement. PHP print return 1 value where as PHP echo do not return any value. These both statements are used for printing output in PHP. The PHP echo or PHP print statement can be used for printing variables, strings, escaping characters etc.  PHP echo and print Statements | PHP Echo Vs Print  This PHP tutorial covers all the topics of Basics of PHP Variable like         1.  PHP Echo Statement         2.  PHP Echo Statement Example         3.  PHP  print   Statement         4.  PHP  print  Statement Example          5.  PHP Echo Vs Print  Now let's start  this tutorial with understanding  PHP Echo Statement with its Examples. In PHP, The output is displayed using either print or echo statements. For printing data, In PHP we can used print or echo statement. There is very small difference between print and echo statement. The print statement return value 1 where as echo do not return any

How to Beginning with your First PHP Program?

Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, I will Cover, How to Write First PHP Program or Script?, How to Execute PHP Program in XAMPP?,What PHP File contains? etc. To Run Your PHP Script, You must be ready with PHP Environment like XAMPP.  If you have not Installed XAMPP till now, You can Visit latest tutorial on  Installation of PHP with XAMPP in Windows 7/ Windows 10  from our blog. By Following instructions given in tutorial, you will be able to download and install XAMPP on Windows 7 / Windows 10. How to Beginning with your First PHP Program? This PHP tutorial covers all the topics of PHP Introduction such as          1. What is PHP Script/ PHP Program?         2. What PHP file Contains?         3. Editors for Writing PHP Program         4. Creating First PHP Program         5. Executing PHP Program in XAMPP      Lets start this PHP tutorial with question What is PHP Script/ PHP Program? What is P

How to create a phpinfo.php page & check PHP Configuration?

Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, I will cover  How to create a phpinfo.php page & check PHP Configuration, What is phpinfo()?, How to use phpinfo() and its example.   You can use a phpinfo() page to view the current Configurations and Settings of PHP information with configured web server. How to create a phpinfo.php page & check PHP Configuration? This PHP tutorial covers all the topics of How to create a phpinfo.php page & check PHP Configuration such as          1. What is PHP phpinfo()?         2. What information  phpinfo() displays?         3. How to use phpinfo()?         4. Example of phpinfo() Let's  start PHP tutorial with questions, What is  PHP phpinfo()? What is PHP phpinfo()? You can use a phpinfo() page to view the current  information  of  Configurations and Settings of PHP with configured web server.  phpinfo() function has an optional argument. If this functio

Installation of PHP with XAMPP in Windows 7/ Windows 10

Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, I will provides in-depth knowledge of Installation of PHP with XAMPP in Windows 7/ Windows 10. XAMPP can be used to install and configure PHP on Windows 7 / Windows 10. Installation of PHP with XAMPP in Windows 7/ Windows 10 This PHP tutorial covers all the topics of Installation of PHP with XAMPP in Windows 7/ Windows 10 such as          1. PHP Installation           2.  What is  XAMPP?         3. Alternative to XAMPP         4. Download XAMPP         5.  Installation of XAMPP in Windows 7/ Windows 10          Lets start PHP tutorial with PHP Installation . PHP Installation As described in our previous post, to install PHP we require three components that are PHP Parser, Web Server and Database Server. Collectively it is called as AMP Software Stack( Apache, MySql, PHP). We can download each component separately from their official websites. After download, w