mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
To supplement an example with an assignment without & for &-function (#4557)
* references.xml To supplement an example with an assignment without & for &-function * Update references.xml Move comments after assignment * Update references.xml Clarify the wording
This commit is contained in:
@@ -490,12 +490,26 @@ function &collector()
|
||||
}
|
||||
|
||||
$collection = &collector();
|
||||
// Now the $collection is a referenced variable that references the static array inside the function
|
||||
|
||||
$collection[] = 'foo';
|
||||
|
||||
print_r(collector());
|
||||
// Array
|
||||
// (
|
||||
// [0] => foo
|
||||
// )
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
<note>
|
||||
<simpara>
|
||||
If the assignment is done without the <literal>&</literal> symbol, e.g. <code>$collection = collector();</code>,
|
||||
the <varname>$collection</varname> variable will receive a copy of the value, not the reference returned by the function.
|
||||
</simpara>
|
||||
</note>
|
||||
To pass the returned reference to another function expecting a reference
|
||||
you can use this syntax:
|
||||
<informalexample>
|
||||
|
||||
Reference in New Issue
Block a user