Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


PHP References explained



One of the most underused features of PHP is the ability to use references. The problem is that using references are rather troublesome in PHP. References was not part of the original syntax but has been added later on, thus leading to a somewhat confusing syntax. There's also little support for references in the native function set.

References in PHP are simpler than the related mechanism found in C and C++, C and C++ uses 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 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 shallow copy.

References are similar to C/C++ pointers but does 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.
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.


| < 1 > | 2 | 3 | 4 | 5 | Next page >> | Printer-friendly page |

Comment List


Topic: Author:
Time:
Definition of large? Coco Loco 12.04.2003 13:17

On page 4, under "When Should References be Used", you say "When you're passing on large text, array or object structures to functions".

What do you consider "large"? It seems to be pretty subjective. Is an array with 100 elements considered "large"? 500? 1000? How about an array with 1 big element?

Thanks for the articles BTW, not many high-level PHP articles around. :)


Misleading Lance Lovette 04.03.2002 19:53

Some of the statements in this article are misleading if you are using PHP 4. You should read the article "PHP 4: Reference Counting and Aliasing" for more perspective - http://www.zend.com/zend/art/ref-count.php. According to that article the term "reference" is more appropriately described as "alias", there is no implicit deep copying on variable assignment and there isn't always a performance gain when passing references as function parameters.


   RE: Misleading Jan Borsodi 11.03.2002 16:08

> Some of the statements in this article are misleading if you
> are using PHP 4. You should read the article "PHP 4:
> Reference Counting and Aliasing" for more perspective -
> http://www.zend.com/zend/art/ref-count.php. According to
> that article the term "reference" is more
> appropriately described as "alias",

Using "alias" can be misleading as well, consider the last example on page 1.
By using "alias" one might believe that $b and $c is a different name for the variable $a and that when $a is unset so are $b and $c, which is not the case.

> there is no
> implicit deep copying on variable assignment and there isn't

Maybe my choice of words were bad but there's still a "copy" going on, whether this is smart and does internal referencing or just copies all the bits is not the point. The point is understanding what references do.

> always a performance gain when passing references as
> function parameters.

I didn't say there always is a performance gain, but that it might.
Allthough PHP 4 has reference counting I have successfully optimized my code by using references several places.
I've also seen the opposite where references would make the code slower.




Forgot your password?

Register a new user

Results

Polls