Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.10     References

This is NOT the latest copy of this book; click here for the latest version.

When you use the = (equals) operator, PHP performs a "copy equals" - it takes the value from operand two and copies it into operand one. While this is fine for most purposes, it doesn't work when you want to be able to change operand two later on and have operand one also change.

Author's Note: as of PHP 5, objects are always passed and assigned by reference by default. Technically, each object has a "handle", which uniquely identifies that object. When you copy an object, you are actually copying this object handle, which means the copy will reference the same object as the original. This was different before PHP 5 - objects were treated like other types of variables and copied entirely when assigned. This led to many programmers inadvertently copying around lots of information in their scripts without realising it, which was wasteful. Therefore, from PHP 5 onwards, objects are always assigned by reference and passed into functions by reference, avoiding the speed hit.

In this situation, references are helpful - they allow you to have two variables pointing to the same data. Using references, setting operand one to equal operand two is instantaneous whether or not operand two is a simple integer or a 10MB array of data.

Furthermore, once two variables are pointing to the same data, you can change either variable and the other one will also update. To assign by reference you need to use the reference operator, &, after the equals operator, giving =&. Here's how it looks:

<?php
    $a
= 10;
    
$b =& $a;
    print
$a;
    print
$b;
    ++
$a;
    print
$a;
    print
$b;
    ++
$b;
    print
$a;
    print
$b;
?>

Here we're using the reference operator to make $b point to the same value as $a, as can be seen in the first two print statements. After incrementing $a, both variables are printed out again, and both are 11, as expected. Finally, to prove that the relationship is two-way, $b is incremented, and again both $a and $b have been updated with the one call.

In the example above, simple integers are used to demonstrate referencing, however references aren't any faster than normal copy assigns for such data because there is so little to copy. Assigning by reference is generally only important if you want to take advantage of the "two variables, one value" paradigm.

Reference assigning is actually slightly slower than copy assigning if you are not using large variables, so it is best to stick with normal copy assigning unless you have good reason to switch.

When it comes to functions, references are also used to allow a function to work directly on a variable rather than on a copy - more on that later.





<< 3.9 Pre-set variables   3.11 Constants: define(), defined(), and constant() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
deathGod - 06 Sep 2008

haha. i actually had a laugh reading this book(maynards mom). to prove paul hudson's point remove the & and your results should be: 10 10 11 10 11 11

Maynards Mom - 06 Sep 2008

I'm loose and son u and nigga suck u bitches

Maynard - 06 Sep 2008

Dude, shut up.

Nigga was right.

A PHP User - 06 Sep 2008

you shouldn't say anything if its not something worth saying A PHP nigga
instead try reading and not saying anything
you would do anyone reading this book a favour by not wasting our time (the time taken to read your silly comments)

A PHP nigga - 06 Sep 2008

This is a gay part of this tutorial, like I will really use this php technique


loloololololzordszzzzz

Skyblaze - 06 Sep 2008

Here he talks, in php 5, about "objects" only. Not all kind of data.

A PHP User - 06 Sep 2008

Is there any way to unlinked the two variables without creating new ones?

Also why would you want two identical and linked variables?

A PHP User - 06 Sep 2008

I was a little confused here. Are you stating that PHP5 does not allow pass-by-value any longer or that it does but not as a default. And is it = that gives the pass by reference or is it =& that gives the pass by reference, please elaborate.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow