PHP &$string - 这是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20419613/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:11:15  来源:igfitidea点击:

PHP &$string - What does this mean?

phpoperands

提问by Mickey

I've been googling but I can't find anything.

我一直在谷歌搜索,但我找不到任何东西。

$x->func(&$string, $str1=false, $str2=false);

what does that &before $string&$stringdo?

&之前有$string&$string什么作用?

回答by Krish R

You are assigning that array value by reference.

您正在通过引用分配该数组值。

passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this copy, so if you change it in the function it won't be changed outside of it

通过引用 (&$) 和 $ 传递参数是当您通过引用传递参数时,您处理原始变量,这意味着如果您在函数内部更改它,它也将在函数外部更改,如果您将参数作为复制,函数创建此变量的复制实例,并在此副本上工作,因此如果您在函数中更改它,则不会在其外部更改

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

参考:http: //www.php.net/manual/en/language.references.pass.php

回答by Rob Baillie

The & states that a reference to the variable should be passed into the function rather than a clone of it.

& 声明对变量的引用应该传递给函数而不是它的克隆。

In this situation, if the function changes the value of the parameter, then the value of the variable passed in will also change.

在这种情况下,如果函数改变了参数的值,那么传入的变量的值也会改变。

However, you should bear in mind the following for PHP 5:

但是,对于 PHP 5,您应该记住以下几点:

  • Call time reference (as you have shown in your example) is deprecated as of 5.3
  • Passing by reference when specified on the function signature is not deprecated, but is no longer necessary for objects as all objects are now passed by reference.
  • 自 5.3 起,不推荐使用呼叫时间参考(如您在示例中所示)
  • 在函数签名上指定时通过引用传递不会被弃用,但对象不再需要,因为所有对象现在都通过引用传递。

You can find more information here: http://www.php.net/manual/en/language.references.pass.php

您可以在此处找到更多信息:http: //www.php.net/manual/en/language.references.pass.php

And there's a lot of information here: Reference — What does this symbol mean in PHP?

这里有很多信息: 参考——这个符号在 PHP 中是什么意思?

An example of the behaviours of strings:

字符串行为的示例:

function changeString( &$sTest1, $sTest2, $sTest3 ) {
    $sTest1 = 'changed';
    $sTest2 = 'changed';
    $sTest3 = 'changed';
}

$sOuterTest1 = 'original';
$sOuterTest2 = 'original';
$sOuterTest3 = 'original';

changeString( $sOuterTest1, $sOuterTest2, &$sOuterTest3 );

echo( "sOuterTest1 is $sOuterTest1\r\n" );
echo( "sOuterTest2 is $sOuterTest2\r\n" );
echo( "sOuterTest3 is $sOuterTest3\r\n" );

Outputs:

输出:

C:\test>php test.php

sOuterTest1 is changed
sOuterTest2 is original
sOuterTest3 is changed

回答by LightningBolt?

&means passing by reference:

&表示按引用传递:

References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand (&) is placed before the variable to be referenced.

引用允许两个变量引用相同的内容。换句话说,一个变量指向它的内容(而不是成为那个内容)。通过引用传递允许两个变量以不同的名称指向相同的内容。与号 ( &) 放在要引用的变量之前。

回答by user3069029

&– Pass by Reference. It is passed by reference instead of string value.

&– 通过引用传递。它是通过引用而不是字符串值传递的。

回答by eX0du5

It means you pass a reference to the string into the method. All changes done to the string within the method will be reflected also outside that method in your code.

这意味着您将对该字符串的引用传递到该方法中。在方法中对字符串所做的所有更改也将反映在代码中该方法之外。

See also: PHP's =& operator

另请参阅:PHP 的 =& 运算符

Example:

例子:

$string = "test";
$x->func(&$string); // inside: $string = "test2";
echo $string; // test2

without the & operator, you would still see "test" in your variable.

如果没有 & 运算符,您仍然会在变量中看到“test”。