Zayfod's example above is useful, but I feel that it needs more explanation. The point that should be made is that a parameter passed in by reference can be changed to reference something else, resulting in later changes to the local variable not affecting the passed in variable:

<?php

function & func_b (){$some_var = 2; return $some_var;}

function func_a (& $param){# $param is 1 here$param = & func_b(); # Here the reference is changed and # the "&" in "func_a (& $param)" # is no longer in effect at all. # $param is 2 here$param++; # Has no effect on $var.}

$var = 1;func_a($var);# $var is still 1 here!!! Because the reference was changed.

I haven't seen anyone note method chaining in PHP5. When an object is returned by a method in PHP5 it is returned by default as a reference, and the new Zend Engine 2 allows you to chain method calls from those returned objects. For example consider this code:

<?php

class Foo {

protected $bar;

public function __construct() {$this->bar = new Bar();

print "Foo\n"; }

public function getBar() { return $this->bar; }}

class Bar {

public function __construct() { print "Bar\n"; }

public function helloWorld() { print "Hello World\n"; }}

function test() { return new Foo();}

test()->getBar()->helloWorld();

?>

Notice how we called test() which was not on an object, but returned an instance of Foo, followed by a method on Foo, getBar() which returned an instance of Bar and finally called one of its methods helloWorld(). Those familiar with other interpretive languages (Java to name one) will recognize this functionality. For whatever reason this change doesn't seem to be documented very well, so hopefully someone will find this helpful.

There is a small exception to the note on this page of the documentation. You do not have to use & to indicate that reference binding should be done when you assign to a value passed by reference the result of a function which returns by reference.

I learned a painful lesson working with a class method that would pass by reference.

In short, if you have a method in a class that is initialed with ampersand during declaration, do not use another ampersand when using the method as in &$this->method();

For example<?phpclass A { public function &hello(){ static $a=''; return $a; } public function bello(){$b=&$this->hello(); // incorrect. Do not use ampersand.$b=$this->hello(); // $b is a reference to the static variable.}?>