PHP 运算符=& 是什么意思?意思是?
我发现了运算符“=&”下面的代码,我不知道什么意思。它是什么意思以及它有什么作用?
我读到的代码:
function ContentParseRoute($segments)
{
$vars = array();
//Get the active menu item
$menu =& JSite::getMenu();
$item =& $menu->getActive();
// Count route segments
$count = count($segments);
....
Possible Duplicate:
What do the "=&" and "&=" operators in PHP mean?
I found the operator "=&" in the following code, and I do not know what it means. What does it mean and what does it do?
The code where I read it:
function ContentParseRoute($segments)
{
$vars = array();
//Get the active menu item
$menu =& JSite::getMenu();
$item =& $menu->getActive();
// Count route segments
$count = count($segments);
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是通过引用 (
&
) 进行的分配 (=
)。如果您说:
您实际上是在将
$a
通过引用分配给$b
。通过引用赋值的作用是将两个变量“绑定”在一起。现在,如果您稍后修改
$a
,$b
也会随之更改。例如:
编辑:
正如 Artefacto 在评论中指出的那样,
$a =& $b
与$a = (&$b)
不相同。这是因为
&
运算符意味着从某些内容中进行引用,而=
运算符则按值进行分配,因此表达式$a = (&$b)
表示对$b
进行临时引用,然后将该临时值赋给$a
,这不是按引用分配。This isn't an assignment (
=
) by reference (&
).If you were to say:
You are actually saying assign
$a
by reference to$b
.What assigning by reference does is "tie" the two variables together. Now, if you were to modify
$a
later on,$b
would change with it.For example:
EDIT:
As Artefacto points out in the comments,
$a =& $b
is not the same as$a = (&$b)
.This is because while the
&
operator means make a reference out of something, the=
operator does assign-by-value, so the expression$a = (&$b)
means make a temporary reference to$b
, then assign the value of that temporary to$a
, which is not assign-by-reference.它是引用赋值运算符。
这意味着当您稍后在代码中修改运算符的 LHS 时,它也会修改 RHS。您将 LHS 指向与 RHS 占用的同一内存块。
It is the referential assignment operator.
This means that when you modify the LHS of the operator later on in code, it will modify the RHS. You are pointing the LHS to the same block of memory that the RHS occupies.
下面是它的使用示例:
不是解释,而是能够使用
&
运算符而不在=&
赋值中使用它的示例。Here's an example of it in use:
Not an explanation, but an example of being able to use the
&
operator without using it in an=&
assignment.