PHP 中的引用赋值运算符,=& [英] Reference assignment operator in PHP, =&

查看:31
本文介绍了PHP 中的引用赋值运算符,=&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

=&(等号-&)赋值运算符在 PHP 中有什么作用?

What does the =& (equals-ampersand) assignment operator do in PHP?

是否已弃用?

推荐答案

它没有被弃用,也不太可能被弃用.例如,这是一种标准方法,可以将一个数组的一部分或对象镜像更改为另一个数组,而不是复制现有数据.

It's not deprecated and is unlikely to be. It's the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data.

它被称为通过引用赋值,引用手册,"意味着两个变量最终都指向相同的数据,任何地方都没有复制任何内容".

It's called assignment by reference, which, to quote the manual, "means that both variables end up pointing at the same data, and nothing is copied anywhere".

唯一不推荐使用 =& 的是在 PHP 5,这可能是任何混淆的根源.new 是通过引用自动分配的,因此 &$o = &new C; 中是多余的/弃用的,但在 中不是>$o = &$c;.

The only thing that is deprecated with =& is "assigning the result of new by reference" in PHP 5, which might be the source of any confusion. new is automatically assigned by reference, so & is redundant/deprecated in$o = &new C;, but not in $o = &$c;.

由于很难搜索,请注意=&(等于&符号)与= &(等于空格&符号)并且经常被写成这样它运行到另一个变量中,如 $x = &$y['z'];$x = &$someVar(& 美元符号变量名).从文档中简化的示例:

Since it's hard to search, note that =& (equals ampersand) is the same as = & (equals space ampersand) and is often written such that it runs into the other variable like $x = &$y['z']; or $x = &$someVar (ampersand dollar sign variable name). Example simplified from the docs:

$a = 3;
$b = &$a;
$a = 4;
print "$b"; // prints 4

<小时>

这是关于按引用分配的详细部分的便捷链接在 PHP 手册中.该页面是参考系列的一部分 - 值得花一分钟阅读整个系列.


Here's a handy link to a detailed section on Assign By Reference in the PHP manual. That page is part of a series on references - it's worth taking a minute to read the whole series.

这篇关于PHP 中的引用赋值运算符,=&amp;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆