Using Comments in PHP
Posted by tutor | Posted in PHP Tutorial | Posted on 11-12-2009-05-2008
0
Comments are used in every programming language for making the code better to understand.
The PHP comment syntax, like other programming language, begins with a special character sequence and all text that appears between the start of the comment and the end will be ignored.
Comments are written to have better readability and understandability of the code. They provide information about the code, or sometimes omits a part of code which may not be used at present ar for a particular scenario like for testing.
A comment’s main purpose is to serve as an informative note to you, the web developer or to others who may view your source code.
PHP’s comments are not displayed to the visitors visiting the website, even if they view the page’s source. The only way to view PHP comments is to open the PHP file for editing. So for this reason, the PHP comments are useful only for the developers, unlike HTML, where the visitor can also view the comments.
PHP Comments is of two types
- Single line – // or # followed by the line to be commented out.
- Multiple lines – /* */ is used to mark the beginning and the end of multiple lined comment.
PHP Comment Syntax: Single Line Comment
The single line comment tells the interpreter to ignore everything that occurs on that line to the right of the comment. To do a single line comment type “//” or “#” and all text to the right will be ignored by PHP interpreter.
<?php
echo “It’s so good to be here!”; // This will print out It’s so good to be here!
echo “<br />There are comments on this page but you can’t see them…!!!”;
// echo “This is not for you”;
// echo “My name is Tutor!”;
# echo “I am the PHP programmer”;
?>
It’s so good to be here!
There are comments on this page but you can’t see them…!!!
Some of our echo statements were not evaluated because we commented them out with the single line comment.
This type of line commenting is often used for quick notes about complex and confusing code or to temporarily remove a line of PHP code, may be for testing.
PHP Comment Syntax: Multiple Line Comment
The multi-line PHP comment can be used to comment out multiple lines of code or writing multiple line comments. The multiple line PHP comment begins with ” /* ” and ends with ” */ “.
<?php
/* This Echo statement will print out my message to the
the place in which I reside on. In other words, the World. */
echo “It’s so good to be here!”;
/* echo “My name is Tutor!”;
echo “I am the PHP programmer!”;
*/
?>
It’s so good to be here!
Good Commenting Practices
The best commenting practice is to use them, anywhere and everywhere, where there is a need to put some extra detail, so that everyone viewing the code can atleast understand the structure.
Use single line comments for quick notes about a tricky part in your code and use multiple line comments when you need to elaborate on some function or some construct.
