PHP 7.1.21 Released


PHP において、リファレンスとは同じ変数の内容を異なった名前で コールすることを意味します。これは C のポインタとは異なります。 リファレンスを使ってポインタの演算をすることはできませんし、 リファレンスは実メモリのアドレスでもありません。詳細は リファレンスが行わないこと を参照ください。 そうではなく、リファレンスはシンボルテーブルのエイリアスです。 PHP では、変数名と変数の内容は異なっており、 このため、同じ内容は異なった複数の名前を有する事が可能であることに 注意してください。最も良く似ているのは、Unix のファイル名とファイルの 関係です。この場合、変数名はディレクトリエントリ、変数の内容は ファイル自体に対応します。リファレンスは、Unix ファイルシステムの ハードリンクのようなものであると考えられます。

add a note add a note

User Contributed Notes 4 notes

273118949 at qq dot com
10 months ago
it just likes a person who has two different names.
26 days ago
Unlike in C, PHP references are not treated as pre-dereferenced pointers, but as complete aliases.

The data that they are aliasing ("referencing") will not become available for garbage collection until all references to it have been removed.

"Regular" variables are themselves considered references, and are not treated differently from variables assigned using =& for the purposes of garbage collection.

The following examples are provided for clarification.

1) When treated as a variable containing a value, references behave as expected. However, they are in fact objects that *reference* the original data.

var = "foo";
$ref1 =& $var; // new object that references $var
$ref2 =& $ref1; // references $var directly, not $ref1!!!!!

echo $ref; // >foo


$ref1; // >Notice:  Undefined variable: ref1
echo $ref2; // >foo
echo $var; // >foo

2) When accessed via reference, the original data will not be removed until *all* references to it have been removed. This includes both references and "regular" variables assigned without the & operator, and there are no distinctions made between the two for the purpose of garbage collection.

= "foo";
$ref =& $var;


$var; // >Notice:  Undefined variable: var
echo $ref; // >foo

3) To remove the original data without removing all references to it, simply set it to null.

= "foo";
$ref =& $var;

$ref = NULL;

$var; // Value is NULL, so nothing prints
echo $ref; // Value is NULL, so nothing prints

4) Placing data in an array also counts as adding one more reference to it, for the purposes of garbage collection.

For more info, see
1 year ago
In summary, "&$reference" means "do-not-copy-on-write the value here, in perpetuity". Assigning by reference is not assignment, it's "make &$variable a reference and its value do-not-copy-on-write, in perpetuity, and make the variable I'm assigning to use that do-not-copy-on-write value as well".

To "unreference/unalias" you have to either unset or make an explicit copy into a new variable.

Object properties that are references will survive cloning and remain references. Generally the same is true with references in arrays and PHP's array functions (combine, intersect, call_user_func, func_get_args, etc).

Calling a function that uses a reference parameter will *make* the supplied variable a reference. This is also true when using variadic array expansion for arguments; the supplier's array element will become a reference.

Generally, don't use them unless you're dealing with low-level calls, or need an accumulator, etc. For poorly designed functions that use them, give them a copy to mangle.
2 years ago
One subtle effect of PHP's assign-by-reference is that operators which might be expected to work with args that are references usually don't.  For example:

$a = ($b ? &$c : &$d);

fails (parser error) but the logically identical

if ($b)
   $a =& $c;
   $a =& $cd;

works. It's not always obvious why seemingly identical code throws an error in the first case. This is discussed on a PHP bug report ( ). TL;DR version, it acts more like an assignment term ($var1) "=&" ($var2) than a function/operator ($var1) "=" (&$var2).
To Top