PHP 运算符=& 是什么意思?意思是?

发布于 2024-09-15 08:34:00 字数 515 浏览 8 评论 0原文

可能的重复: “=&”的作用是什么和“&=”运算符在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2024-09-22 08:34:00

这不是通过引用 (&) 进行的分配 (=)。

如果您说:

$a = 42;
$b =& $a;

实际上是在将$a通过引用分配给$b

通过引用赋值的作用是将两个变量“绑定”在一起。现在,如果您稍后修改 $a$b 也会随之更改。

例如:

$a = 42;
$b =& $a;

//later
echo $a; // 42
echo $b; // 42

$a = 13;
echo $a; // 13
echo $b; // 13

编辑:

正如 Artefacto 在评论中指出的那样, $a =& $b$a = (&$b)相同。

这是因为 & 运算符意味着从某些内容中进行引用,而 = 运算符则按进行分配,因此表达式$a = (&$b) 表示对 $b 进行临时引用,然后将该临时值赋给 $a,这不是按引用分配。

This isn't an assignment (=) by reference (&).

If you were to say:

$a = 42;
$b =& $a;

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:

$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 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.

救赎№ 2024-09-22 08:34:00

它是引用赋值运算符

这意味着当您稍后在代码中修改运算符的 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.

从来不烧饼 2024-09-22 08:34:00

下面是它的使用示例:

$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

不是解释,而是能够使用 & 运算符而不在 =& 赋值中使用它的示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文