HTML and CSS Reference
In-Depth Information
The text that precedes // is processed by PHP, so the second line assigns the $color
variable. On the third line, I've turned off the assignment by commenting it out. PHP
also supports multiple-line comments, which begin with /* and end with */ . If you want
to comment out several lines of code, you can do so like this:
/*
$color = 'red';
$count = 55; // Set the number of items on a page.
// $count = $count + 1;
*/
PHP ignores all the lines inside the comments. Note that you can put the // style com-
ment inside the multiline comment with no ill effects. You cannot, however, nest multi-
line comments. This is illegal:
/*
$color = 'red';
$count = 55; // Set the number of items on a page.
/* $count = $count + 1; */
*/
NOTE
The generally accepted style for PHP code is to use // for single-
line comments rather than # .
Variables
Variables just provide a way for the programmers to assign a name to a piece of data. In
PHP, these names are preceded by a dollar sign ( $ ). Therefore, you might store a color in
a variable called $color or a date in a variable named $last_published_at . Here's how
you assign values to those variables:
$color = “red”;
$last_published_at = time();
The first line assigns the value “red” to $color ; the second returns the value returned by
the built-in PHP function time() to $last_published_at . That function returns a time-
stamp represented as the number of seconds since the “UNIX epoch.”
21
One thing you should notice here is that you don't have to indicate what kind of
item you'll be storing in a variable when you declare it. You can put a string in it,
as I did when I assigned “red” to $color . You can put a number in it, as I did with
$last_published_at . I know that the number is a timestamp, but as far as PHP is
 
Search WWH ::




Custom Search