Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
coding standards
communitycreator

How to add comments in PHP

NDUKWE CHIDERA K.

A comment is a simple piece of explanatory text added to a program code to facilitate the understanding of such codes by other users. These comments are ignored by the computer during the code execution.

Why add comments to your code?

The following are some of the industry-wide reasons as to why codes should be commented on.

  • It serves as an integral part of main program documentation.
  • It aids smooth source code handover between programmers.
  • It reduces the time spent in understanding what a particular code block does in a program, especially by other users.
  • Comments are second nature for a collaborative team.
  • Your stress during debugging will be drastically reduced.
  • Comments are part of the generally accepted coding standards in PHP. To know more about these standards, see this shot.

Comments in PHP

In the PHP programming language, comments in code can be done in three ways. These commenting styles are discussed below.

Inline commenting

An inline comment is a type of comment that starts and ends in a single line. For this comment type, we have two comment styles:

1. Using the PERL style

This style of commenting is native to the PERL programming language, and it involves writing your comments after the hash (#) symbol just like so:

<?php
#This is an inline comment
?>

2. Using the C++ comments style

This style is native to the C++ programming language. It is the most used in PHP for inline comments. With this style, your comments come after double slashes (//) just like so:

<?php
//Also an inline comment
?>

Multi-line commenting

The multi-line comment spills into multiple lines. It can be achieved only with the style discussed below.

3. The C commenting style

With this style, you can have commented that span over several lines in your program code. This style involves the use of a beginning forward-slash (/) followed by asterisks (*), like this (/*), and an ending asterisk (*) followed by the forward-slash (/) symbol, like this (*/). See below:

<?php
/*
This is 
 a simple 
 multiline comment
*/
?>

Code

This code shows all comment styles in use and how the PHP parser skipped whatever is in the comment and ran only the code.

<?php
// Let us greet the world with this(single line)
$greeting = "Hello! World";
# Let do some math (single line)
$maths = 50 +60*2 / 28;
/* 
*The above calculation has this output:54.285714285714
so far the parse will disregard all of these comments
echo all varibles to string down below
*/
echo $greeting ."\n";
echo $maths;
?>

Some tips on commenting

  • You would do well to avoid the use of the PERL comment style (#). This is not because it is bad, but because it can be interpreted differently based on the platform.
  • Make your comments short, simple, and concise.
  • For lists in your comments, it is best to use the multi-line comment so that each list item can be on a separate line.
  • For most IDEs and code editors in use today, you can comment out a large code block by highlighting a block and pressing the ctrl+/ keys on a PC and ⌘+/ keys on a MAC.

RELATED TAGS

php
coding standards
communitycreator
RELATED COURSES

View all Courses

Keep Exploring