Therefore, a change to any variable referencing a particular item of variable content
will be reflected among all other variables referencing that same content. You can
assign variables by reference by appending an ampersand (&) to the equal sign. Let??™s
consider an example:
$value1 = "Hello";
$value2 =& $value1; // $value1 and $value2 both equal "Hello"
$value2 = "Goodbye"; // $value1 and $value2 both equal "Goodbye"
?>
An alternative reference-assignment syntax is also supported, which involves
appending the ampersand to the front of the variable being referenced. The following
example adheres to this new syntax:
76 CHAPTER 3 ?– PHP B ASICS
$value1 = "Hello";
$value2 = &$value1; // $value1 and $value2 both equal "Hello"
$value2 = "Goodbye"; // $value1 and $value2 both equal "Goodbye"
?>
References also play an important role in both function arguments and return
values, as well as in object-oriented programming. Chapters 4 and 6 cover these
features, respectively.
Variable Scope
However you declare your variables (by value or by reference), you can declare them
anywhere in a PHP script. The location of the declaration greatly influences the realm
in which a variable can be accessed, however. This accessibility domain is known as
its scope.
PHP variables can be one of four scope types:
??? Local variables
??? Function parameters
??? Global variables
??? Static variables
Local Variables
A variable declared in a function is considered local.
Pages:
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161