From 6345094341ed05b153cec937baffde621c98aa75 Mon Sep 17 00:00:00 2001 From: Mikhail Alferov Date: Sun, 23 Mar 2025 10:00:49 +0300 Subject: [PATCH 1/3] references.xml To supplement an example with an assignment without & for &-function --- language/references.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/language/references.xml b/language/references.xml index 76a1210bfc87..f8f0b5ebf413 100644 --- a/language/references.xml +++ b/language/references.xml @@ -489,13 +489,26 @@ function &collector() return $collection; } +// Now the $collection is a reference to the static array inside the function $collection = &collector(); $collection[] = 'foo'; +print_r(collector()); +// Array +// ( +// [0] => foo +// ) + ?> ]]> + + + If the assignment is done without the & symbol, e.g. $collection = collector();, + the $collection variable will receive a copy of the value, not the reference returned by the function. + + To pass the returned reference to another function expecting a reference you can use this syntax: From 33fc092d06adee6ff107cc287121c2b9f13526c4 Mon Sep 17 00:00:00 2001 From: Mikhail Alferov Date: Sun, 23 Mar 2025 10:06:36 +0300 Subject: [PATCH 2/3] Update references.xml Move comments after assignment --- language/references.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/language/references.xml b/language/references.xml index f8f0b5ebf413..dbf09be1d481 100644 --- a/language/references.xml +++ b/language/references.xml @@ -489,8 +489,9 @@ function &collector() return $collection; } -// Now the $collection is a reference to the static array inside the function $collection = &collector(); +// Now the $collection is a reference to the static array inside the function + $collection[] = 'foo'; print_r(collector()); From 07aa4beab1f9f9904c9acc262969456d30452d4a Mon Sep 17 00:00:00 2001 From: Mikhail Alferov Date: Sun, 23 Mar 2025 10:25:50 +0300 Subject: [PATCH 3/3] Update references.xml Clarify the wording --- language/references.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/references.xml b/language/references.xml index dbf09be1d481..7eafdfacfcdb 100644 --- a/language/references.xml +++ b/language/references.xml @@ -490,7 +490,7 @@ function &collector() } $collection = &collector(); -// Now the $collection is a reference to the static array inside the function +// Now the $collection is a referenced variable that references the static array inside the function $collection[] = 'foo';