Freelance PHP programmer India | Freelance web developer india | Web development India | web developer CV india | Compiling PHP and Apache 2 from source on Linux
References Explained
PHP Manual

Passing by Reference

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:

<?php
function foo(&$var)
{
    
$var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

Note that there's no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. In recent versions of PHP you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.

The following things can be passed by reference:

No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:

<?php
function bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces fatal error since PHP 5.0.5

foo($a 5); // Expression, not variable
foo(5); // Produces fatal error
?>

These requirements are for PHP 4.0.4 and later.


References Explained
PHP Manual