PHP:$var 和 &$var 有什么区别?
有什么不一样
foreach ($my_array as $my_value) {
}
和: 和
foreach ($my_array as &$my_value) {
}
?
我可以请您举两个现实世界的例子来说明何时使用其中一种以及何时使用另一种吗?
What is the difference between
foreach ($my_array as $my_value) {
}
And:
foreach ($my_array as &$my_value) {
}
?
May I ask you to give me two real-world examples of when to use one and when you use the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
第一个示例创建值的副本,而第二个示例使用对原始值的引用。因此,在第一个
foreach
运行后,原始数组仍然保持不变。在第二个foreach
之后,原始数组可能已被修改,因为它是通过引用处理的。一些原生 PHP 函数已经以这种方式工作,例如
shuffle()< /code>
重新排列数组的内容。您会注意到这个函数不返回一个数组,您只需调用它即可:
它发挥了它的魔力,因为它通过引用来处理数组而不是创建它的副本。
还有一些函数不通过引用传递任何内容,而是处理原始值的副本,例如
ucwords()
返回新的结果字符串:请参阅 通过引用传递。
The first example creates a copy of the value, whereas the second uses a reference to the original value. So after the first
foreach
runs, the original array is still untouched. After the secondforeach
the original array could have been modified since it was handled by reference.Some native PHP functions already work this way, such as
shuffle()
which rearranges the contents of your array. You'll notice that this function doesn't return an array, you just call it:And it works its magic since it works with the array by reference rather than creating a copy of it.
Then there are functions that don't pass anything by reference but rather deal with a copy of the original value, such as
ucwords()
which returns the new resulting string:See Passing by Reference.
Jonathan 的回答很好地描述了这一点。为了完整起见,这里有两个示例:
仅读取值:
向每个元素添加一些数字(从而修改每个值):
如果您不使用
&$my_value
,那么添加不会对$my_array
产生任何影响。但是您可以不使用引用来编写相同的内容:
不同之处在于,我们直接使用
$my_array[$key]
访问/更改原始值。演示
Jonathan's answers describes it very well. Just for completeness, here are your two examples:
Just reading values:
Adding some number to each element (thus modifying each value):
If you don't use
&$my_value
, then the addition won't have any effect on$my_array
.But you could write the same not using references:
The difference is that we are accessing/changing the original value directly with
$my_array[$key]
.DEMO
当您在变量前加上 & 符号时,您就创建了一个“引用”。 PHP 引用就像计算机上的快捷方式或符号链接。您可以创建一个指针变量,它只是相同数据的另一个名称。
我没有看到使用这些有什么大的区别,除了你不复制保存内存的变量。当您传递变量时,您可以只传递引用和引用指向原始对象。
When you prefix a variable with an ampersand, you’re creating a “reference.” PHP references are like shortcuts or symlinks on your computer. You can create a pointer variable that is just another name for the same data.
I dont see a big difference in using these, except that you DONT COPY a variable saving memory. When you are passing variables you can just pass the reference and the reference points to the original object.
一个现实世界的例子 &当您需要用很少的代码行更改数组的内容时使用
A real world example for & use is when you need to change the content of an array with very few lines of code
我仅在必须读取 csv 文件才能知道分隔符是什么时才使用引用。只需取第一行:
然后使用此可能的分隔符
通过引用计数最常用的分隔符
I only use references when i have to read csv files to know what is the delimitier. Just take the first line:
Then with this possible delimitiers
Count by reference what is the most used