PHP References Explained - What is a Reference?
(Page 2 of 4 )
References in PHP are simpler than the related mechanism found in C and C++. C and C++ use pointers, which allows for pointing to memory areas. C++ also has references which are less troublesome than pointers but not as versatile. By using references, you can refer or point to the same data using different variables. This is useful when you want to avoid costly memory operations or want functions to access your data directly rather than by copying it.
References are created by assigning a variable with the reference operator, &:
$a = "data";
$b =& $a; // Now $b refers to data in $a As I mentioned above, the behavior is not as advanced as in C/C++. This means that you cannot create a reference to a reference. For instance, let's extend the above example by create a new reference:
$c =& $b; Now $c will refer to the same data as $b, i.e the reference has been copied but not the data. This means that we've done a cheap copy of the data, sometimes known as a shallow copy.
References are similar to C/C++ pointers, but do not use a pointer to the content. The difference should be clearer in this example:
$a = "test";
$b =& $a;
$c =& $b;
$a = "ok";
print( "$a $b $c" ); // outputs ok ok ok
$d = "test";
$a =& $d;
print( "$a $b $c" ); // outputs test ok ok As you can see, both $b and $c refer to the content of $a, but when $a is changed into a reference they still point to the same content as before – i.e. they are not referring to what $a is referring to.
The same thing happens with the unset function:
$a = "test";
$b =& $a;
$c =& $b;
unset( $a );
print( "$b $c" ); // outputs test test $b and $c still point to the same content as before the unset.
Arrays and References You can also refer to array elements as references:
$a = array();
$b = "test";
$a[0] =& $b;
$a[1] = "ok";
$a["ok"] =& $b; Now let's get back to the standard assignment operator, consider this code:
$d = $a; This creates a new copy of the data and refers to it in $d. This is known as a deep copy, and is what you've probably been using when you code with PHP. References are extremely important when you're working with complex objects and array hierarchies.
All of PHP's iterating (repeating) structures and functions do not create references, but instead they actually copy each element. This is OK when dealing with small data sizes, but has a significant performance hit with larger data.
Arrays have a different behavior when using the assignment operator:
$b = "data";
$arr = array();
$arr["a"] =& $b;
$arr_copy = $arr; This actually performs a copy of all of the elements of the array but only a shallow copy, so the reference is only copied to the new element -- not the data.
Using the function var_dump, you can easily spot elements that are references.
This means that array elements will be deep copied if they have real contents, but will have a shallow copy when a reference is encountered.
Next: References and Functions >>
More PHP Articles
More By Jan Borsodi