PHP Tutorial - Comments
PHP Tutorial - Comments
Comments in PHP are similar to comments that are used in HTML. A comment is started with a special character sequence and all text that appears between the start of the comment and the end will be ignored by the browser.
In HTML, a comment's only purpose is to serve as a note to you, the web developer or to others who may view your website's source. However, PHP's comments are different in that they will not be displayed to your visitors. The only way to view PHP comments is to open the file for editing, which makes PHP comments useful only PHP programmers.
In case you forgot what an HTML comment looked like, see our example below.
<!--- This is an HTML Comment -->
Single Line Comment
In HTML there was only one kind of comment. However, in PHP there are two kinds. The first we will discuss is the single line comment. The single line comment tells the interpreter to ignore everything that occurrs on that line, to the right of the comment. To do a single line comment type "//" and all text that follows will be commented out.
Notice that a couple 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.
<?php
echo "Hello World!"; // This will print out Hello World!
echo "<br />Psst...You can't see my PHP comments!"; // echo "nothing";
// echo "My name is Humperdinkle!";
?>
Multiple Line Comment
Similiar to the HTML comment, the multiple line PHP comment can be used to comment out large blocks of code or writing multiple line comments. The multiple line PHP comment begins with " /* " and ends with " */ ".
/* This Echo statement will print out my message to the
the place in which I reside on. In other words, the World. */
echo "Hello World!";
/* echo "My name is Humperdinkle!";
echo "No way! My name is Uber PHP Programmer!";
*/
?>
Good Commenting Practices
One of the best commenting practices that I can recommend to new PHP programmers is....USE THEM!! So many people write complex PHP code and are either too lazy to write good comments or believe the commenting is not needed. However, do you really think that you will remember exactly what you were thinking when looking at the code a year or more later?
Let the comments permeate your code and you will be a happier PHPer in the future. Use single line comments for quick notes about a tricky part in your code and use multiple line comments when you need to describe something in greater depth than a simple note.