Lexical Structure
|
19
Comments
Comments give information to people who read your code, but they are ignored by
PHP. Even if you think you’re the only person who will ever read your code, it’s a
good idea to include comments in your code—in retrospect, code you wrote months
ago can easily look as though a stranger wrote it.
Good practice is to make your comments sparse enough not to get in the way of the
code itself and plentiful enough that you can use the comments to tell what’s hap-
pening. Don’t comment obvious things, lest you bury the comments that describe
tricky things. For example, this is worthless:
$x = 17; // store 17 into the variable $x
whereas this may well help whoever will maintain your code:
// convert nnn; entities into characters
$text = preg_replace('/([0-9])+);/e', "chr('\\1')", $text);
PHP provides several ways to include comments within your code, all of which are bor-
rowed from existing languages such as C, C++, and the Unix shell. In general, use C-
style comments to comment
out
code, and C++-style comments to comment
on
code.
When PHP encounters a hash mark (
#
) within the code, everything from the hash mark
to the end of the line or the end of the section of PHP code (whichever comes first) is
considered a comment. This method of commenting is found in Unix shell scripting
languages and is useful for annotating single lines of code or making short notes.
Because the hash mark is visible on the page, shell-style comments are sometimes
used to mark off blocks of code:
#######################
## Cookie functions
#######################
Sometimes they’re used before a line of code to identify what that code does, in
which case they’re usually indented to the same level as the code:
if ($double_check) {
# create an HTML form requesting that the user confirm the action
echo confirmation_form( );
}
Short comments on a single line of code are often put on the same line as the code:
$value = $p * exp($r * $t); # calculate compounded interest
When you’re tightly mixing HTML and PHP code, it can be useful to have the clos-
ing PHP tag terminate the comment:
Then another
Then another 4
,ch02.15294 Page 19 Wednesday, March 13, 2002 11:42 AM
This is the Title of the Book, eMatter Edition
Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.
Do'stlaringiz bilan baham: |