Home arrow PHP arrow Page 2 - PHP References Explained
PHP

PHP References Explained


C/C++ programmers have used references for years, but did you know that you can also use references in PHP? In this article Jan explains what references are and exactly how to use them with PHP...

Author Info:
By: Jan Borsodi
Rating: 3 stars3 stars3 stars3 stars3 stars / 6
December 22, 2002
TABLE OF CONTENTS:
  1. · PHP References Explained
  2. · What is a Reference?
  3. · References and Functions
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials