Comments in PHP project is added for the purpose to make it easy to understand the code at the time of maintenance or reuse after long time. And, it also helps in such situation, when another developer takes over our code and continues with its remaining development. So, this will help the developer for future reference, rather, it won’t be taken for execution process.
PHP supports various types of comment style which falls into two main category listed below.
- Single line comment style
- Multi-line comment style
The both styles are provided by PHP whereas other languages like C, C++ and Shell script provides any one of it. Let us see these types in detail.
Single line comment style
This type of comment will be added by the use of either double slashes(//) symbol as C++, or by hash(#) symbol as shell script. As per it’s name of this comment style, it comments only a line before which any one of the above two symbols are added. If the commented line is embedded with some PHP block of code, then comment line will be skipped after this end of PHP block though statement continues further. Following example will help us to catch this point easier.
<?php //To write PHP program echo "Single line comment"; ?>
After running this program, it will return the echo statement to the browser, without executing the comment line. Whereas, the following program will throw an error, since the comment line is end with the end of php statement.
<?php //To write PHP program echo "Single line comment"; //To write a PHP program as <?php echo "Comments" ; ?> which prints Comments ?>
To overcome this problem, the second type of comment style is used. And, we can replace ‘//’ with ‘#’ which will create same result.
Multi-line comment style
This type of comment is used to comment some block of more lines. Set of statement to be commented should be enclosed within /* and */. Unlike single line style, this comment line will not be truncated with the symbols like double slashes(//), hash(#) or PHP close delimeter(?>) occurs in between.This is shown in following code sample.
<?php /* To write PHP program //to print by writing as # <?php echo "Multi-line line comment" ; ?> which prints Comments */ echo "Multi-line line comment"; ?>
But, nested comments can not be possible. for example,
<?php /* To write PHP program /*to print by writting as # <?php echo "Multi-line line comment" ; ?> */ which prints Comments */ echo "Multi-line line comment"; ?>
will create error, while running this program.
0 comments:
Post a Comment