PHP中“&”符号的含义是什么? [重复]

时间:2021-05-02 16:58:35

This question already has an answer here:

这个问题在这里已有答案:

I was trying to find this answer on Google, but I guess the symbol & works as some operator, or is just not generally a searchable term for any reason.. anyhow. I saw this code snippet while learning how to create WordPress plugins, so I just need to know what the & means when it precedes a variable that holds a class object.

我试图在谷歌上找到这个答案,但我猜这个符号和作为一些运算符的作用,或者因为任何原因通常不是一个可搜索的术语..无论如何。我在学习如何创建WordPress插件时看到了这段代码片段,因此我只需要知道它在包含类对象的变量之前的含义。

//Actions and Filters
if (isset($dl_pluginSeries)) {

    //Actions
    add_action('wp_head', array(&$dl_pluginSeries, 'addHeaderCode'), 1);
    //Filters
    add_filter('the_content', array(&$dl_pluginSeries, 'addContent'));
}

4 个解决方案

#1


19  

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

这将强制变量通过引用传递。通常,将为简单类型创建硬拷贝。对于大字符串(性能增益)或者如果您想在不使用return语句的情况下操作变量,这可以派上用场,例如:

$a = 1;

function inc(&$input)
{
   $input++;
}

inc($a);

echo $a; // 2

Objects will be passed by reference automatically.

对象将自动通过引用传递。

If you like to handle a copy over to a function, use

如果您想要复制到函数,请使用

clone $object;

Then, the original object is not altered, eg:

然后,原始对象不会改变,例如:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1

#2


8  

The ampersand preceding a variable represents a reference to the original, instead of a copy or just the value.

变量前面的&符号表示对原始数据的引用,而不是副本或仅仅是值。

See here: http://www.phpreferencebook.com/samples/php-pass-by-reference/

见这里:http://www.phpreferencebook.com/samples/php-pass-by-reference/

#3


7  

This passes something by reference instead of value.

这通过引用而不是值传递。

See:

http://php.net/manual/en/language.references.php
http://php.net/manual/en/language.references.pass.php

#4


3  

I used it for sending a variable to a function, and have the function change the variable around. After the function is done, I don't need to return the function to the return value and set the new value to my variable.

我用它来向函数发送变量,并让函数改变变量。函数完成后,我不需要将函数返回到返回值并将新值设置为我的变量。

Example

function fixString(&$str) {
    $str = "World";
}

$str = "Hello";
fixString($str);
echo $str; //Outputs World;

Code without the &

function fixString($str) {
    $str = "World";
    return $str;
}

$str = "Hello";
$str = fixString($str);
echo $str; //Outputs World;

#1


19  

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

这将强制变量通过引用传递。通常,将为简单类型创建硬拷贝。对于大字符串(性能增益)或者如果您想在不使用return语句的情况下操作变量,这可以派上用场,例如:

$a = 1;

function inc(&$input)
{
   $input++;
}

inc($a);

echo $a; // 2

Objects will be passed by reference automatically.

对象将自动通过引用传递。

If you like to handle a copy over to a function, use

如果您想要复制到函数,请使用

clone $object;

Then, the original object is not altered, eg:

然后,原始对象不会改变,例如:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1

#2


8  

The ampersand preceding a variable represents a reference to the original, instead of a copy or just the value.

变量前面的&符号表示对原始数据的引用,而不是副本或仅仅是值。

See here: http://www.phpreferencebook.com/samples/php-pass-by-reference/

见这里:http://www.phpreferencebook.com/samples/php-pass-by-reference/

#3


7  

This passes something by reference instead of value.

这通过引用而不是值传递。

See:

http://php.net/manual/en/language.references.php
http://php.net/manual/en/language.references.pass.php

#4


3  

I used it for sending a variable to a function, and have the function change the variable around. After the function is done, I don't need to return the function to the return value and set the new value to my variable.

我用它来向函数发送变量,并让函数改变变量。函数完成后,我不需要将函数返回到返回值并将新值设置为我的变量。

Example

function fixString(&$str) {
    $str = "World";
}

$str = "Hello";
fixString($str);
echo $str; //Outputs World;

Code without the &

function fixString($str) {
    $str = "World";
    return $str;
}

$str = "Hello";
$str = fixString($str);
echo $str; //Outputs World;