Wednesday, May 23, 2012

Using Comments to Make Code More Readable

To round off this chapter, you learn about another basic feature of PHP: comments . A comment is simply text that is ignored by the PHP engine. The purpose of comments is to let you add messages to yourself (and other programmers) that explain what your code does. It ’ s always a good idea to add comments to your code, even if you ’ re the only programmer working on it. Sometimes code that makes sense when you write it can seem as clear as mud in three months ’ time, so comments can really help!
PHP supports single - line comments and multi - line comments. To write a single - line comment, start the line with either two slashes (//) or a hash symbol (#). For example:
// This code displays the current time
# This code displays the current time
To write multi - line comments, start the comment with a slash followed by an asterisk (/*) and end the comment with an asterisk followed by a slash (*/), as follows:
/*
This code displays the
current time in a nice,
easy-to-read format.
*/
So you might comment the PHP code in the hello_with_time.php script like this:
< ?php
// Get the current time in a readable format
$currentTime = date( “g:i:s a” );
// Display greeting and time to the visitor
echo “Hello, world! The current time is $currentTime”;
? >