For instance, the following variables bear
absolutely no relation to one another:
??? $color
??? $Color
??? $COLOR
Interestingly, variables do not have to be explicitly declared in PHP as they do in
Perl. Rather, variables can be declared and assigned values simultaneously. Nonetheless,
just because you can do something doesn??™t mean you should. Good programming
practice dictates that all variables should be declared prior to use, preferably with an
accompanying comment.
CHAPTER 3 ?– PHP BASICS 75
Once you??™ve declared your variables, you can begin assigning values to them. Two
methodologies are available for variable assignment: by value and by reference. Both
are introduced next.
Value Assignment
Assignment by value simply involves copying the value of the assigned expression to
the variable assignee. This is the most common type of assignment. A few examples
follow:
$color = "red";
$number = 12;
$age = 12;
$sum = 12 + "15"; // $sum = 27
Keep in mind that each of these variables possesses a copy of the expression assigned
to it. For example, $number and $age each possesses their own unique copy of the value 12.
If you prefer that two variables point to the same copy of a value, you need to assign by
reference, introduced next.
Reference Assignment
PHP 4 introduced the ability to assign variables by reference, which essentially means
that you can create a variable that refers to the same content as another variable does.
Pages:
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160