What does it mean to start a PHP function with an ampersand?
An ampersand before a function name means the function will return a reference to a variable instead of the value.
Returning by reference is useful when
you want to use a function to find to
which variable a reference should be
bound. Do not use return-by-reference
to increase performance. The engine
will automatically optimize this on
its own. Only return references when
you have a valid technical reason to
do so.
See Returning References.
What's the purpose of using & before a function's argument?
It's a pass by reference. The variable inside the function "points" to the same data as the variable from the calling context.
function foo(&$bar)
{
$bar = 1;
}
$x = 0;
foo($x);
echo $x; // 1
An Ampersand (&) before a function means?
It returns the result by reference. See the manual entry for it here.
What does prepending '&' to a function name mean in PHP?
It means the function should return a reference to a variable rather than just the value itself.
How does the & operator work in a PHP function?
The &
operator tells PHP not to copy the array when passing it to the function. Instead, a reference to the array is passed into the function, thus the function modifies the original array instead of a copy.
Just look at this minimal example:
<?php
function foo($a) { $a++; }
function bar(&$a) { $a++; }
$x = 1;
foo($x);
echo "$x\n";
bar($x);
echo "$x\n";
?>
Here, the output is:
1
2
– the call to foo
didn’t modify $x
. The call to bar
, on the other hand, did.
Do I need to use the ampersand in PHP 5.5.X and above anymore?
The reason different articles seem to be saying different things is that they are talking about different kinds of pass-by-reference.
The main thing that determines if a parameter should be passed by reference is the function signature itself, and the fundamentals of this have not changed since PHP 4. Consider this example:
function foo( $by_value, &$by_reference ) { /* ... */ }
$a = 1; $b = 2;
foo( $a, $b );
Here, the outer variable $a
is being passed to the function by value, as though it is being assigned as $by_value = $a;
- changes to $by_value
cannot affect $a
. The variable $b
however is being passed by reference; just like an assignment of the form $by_reference =& $b;
this means that there is one variable referenced by two names, and any assignment to one will act as an assignment to both.
If you pass an "ordinary" value (a string, number, or array) by value, its value is just copied to the new variable. As of PHP 5: If you pass an object by value, however, something slightly different happens - the "value" copied is just a pointer to the same object. This means that if $a
were an object, you could call $by_value->some_property = 42;
and $a->some_property
would also be 42
. However, if you assigned some new value to $by_value
, it would still not affect $a
.
Until PHP 5.4, there was an extra way to pass a parameter by reference, which was to "force" the reference behaviour at call-time. This meant that you could write foo(&$a, &$b);
and "capture" changes made to $by_value
inside the foo()
function. Relying on this was generally a bad idea, and so it was removed. (It landed in 5.4 because it was intended for removal in PHP 6, but that project was put on indefinite hold, with the smaller changes landing in 5.3 and 5.4).
Finally, functions can return a variable by reference (as discussed in the manual here). This is a little fiddly, as it actually requires you to put &
in two places: at the beginning of the function declaration, to say that return
should mean "return this variable reference" not "return this value"; and in the code calling it, to assign a variable to that reference, rather than just copying its value. Here's a silly example which combines a reference parameter with a reference return (the two do not have to go together, it's just an example):
function &bar(&$some_param) { return $some_param; }
$a = 1;
$b =& bar($a);
// $b and $a now point at the same variable, not just the same value
// it was passed into and out of a function, and assigned to a new variable,
// but all those operations were by reference
Note that many people mistakenly believe that passing a variable by reference will give them a performance benefit, and this was often their only reason for using call-time pass-by-reference. This is in fact usually wrong, as the Zend Engine which powers PHP uses a technique called "copy on write" to leave multiple variables which happen to have the same value pointing at the same piece of memory, even if they are not bound up as references. In fact, reference assignment generally defeats this optimisation, due to the way the engine tracks which variables are in the copy-on-write state.
difference between function and &function
If you put an ampersand before your function name, the function will return a reference to a variable instead of the value.
Returning by reference is useful when you want to use a function to
find to which variable a reference should be bound. Do not use
return-by-reference to increase performance. The engine will
automatically optimize this on its own. Only return references when
you have a valid technical reason to do so.
From the PHP Manual on Returning References.
Practical uses of prepending an ampersand to PHP variables
$original = 'apples';
function foo($word) {
$word = 'oranges';
}
foo($original);
echo $original; // apples, because only local $word was changed, not $original.
foo(&$original);
echo $original; // oranges, because $original and $word are the same
Related Topics
How to Minify PHP Page HTML Output
Split String by New Line Characters
Create Word Document Using PHP in Linux
Laravel: Syntax Error or Access Violation: 1055 Error
Execute PHP Script in Cron Job
"The Page Has Expired Due to Inactivity" - Laravel 5.5
How to Get Multiple Parameters With Same Name from a Url in PHP
Get Sum of MySQL Column in PHP
Glob() - Sort Array of Files by Last Modified Datetime Stamp
Preserving Session Variables Across Different Domains
Strict Standards: MySQLi_Next_Result() Error With MySQLi_Multi_Query
Print Numeric Values to Two Decimal Places
PHPdoc Type Hinting For Array of Objects
How to Post Pictures to Instagram Using API
Pdo Parameterized Query - Reuse Named Placeholders
How to Create an Array from a CSV File Using PHP and the Fgetcsv Function