PHP
  Home arrow PHP arrow Page 2 - PHP References Explained
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP References Explained
By: Jan Borsodi
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2002-12-22

    Table of Contents:
  • PHP References Explained
  • What is a Reference?
  • References and Functions
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By Jan Borsodi


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway