PHP 运算符 =& 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3526555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the PHP operator =& mean?
提问by IberoMedia
Possible Duplicate:What do the "=&" and "&=" operators in PHP mean?
可能的重复:PHP 中的“=&”和“&=”运算符是什么意思?
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);
....
回答by Austin Hyde
This isn't an assignment (=
) by reference (&
).
这不是=
通过引用 ( &
)进行的赋值( )。
If you were to say:
如果你要说:
$a = 42;
$b =& $a;
You are actuallysaying assign $a
by referenceto $b
.
您实际上是说$a
通过引用分配给$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.
通过引用赋值的作用是将两个变量“绑定”在一起。现在,如果你$a
以后要修改,$b
就会随之改变。
For example:
例如:
$a = 42;
$b =& $a;
//later
echo $a; // 42
echo $b; // 42
$a = 13;
echo $a; // 13
echo $b; // 13
EDIT:
编辑:
As Artefacto points out in the comments, $a =& $b
is notthe same as $a = (&$b)
.
作为Artefacto在评论中指出的,$a =& $b
是不一样的$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 notassign-by-reference.
这是因为虽然&
运算符的意思是从某事物中进行引用,但=
运算符确实是按值赋值的,因此表达式的$a = (&$b)
意思是对 进行临时引用$b
,然后将该临时值分配给$a
,这不是按引用赋值.
回答by Jacob Relkin
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.
这意味着当您稍后在代码中修改运算符的 LHS 时,它将修改 RHS。您将 LHS 指向 RHS 占用的同一块内存。
回答by bschaeffer
Here's an example of it in use:
这是它的使用示例:
$array = array('apple', 'orange', 'banana');
// Without &
foreach($array as $d)
{
$d = 'fruit';
}
echo implode(', ', $array); // apple, orange, banana
// With &
foreach($array as &$d)
{
$d = 'fruit';
}
echo implode(', ', $array); // fruit, fruit, fruit
Not an explanation, but an example of being able to use the &
operator without using it in an =&
assignment.
不是解释,而是一个能够使用&
运算符而不在=&
赋值中使用它的例子。