php end()并通过参考

发布于 2025-01-18 03:18:18 字数 335 浏览 2 评论 0原文

我尝试将爆炸语句传递到 end() 中,并从 IDE 中收到以下通知:“只有变量可以通过引用传递”,这并不奇怪。

然而,令我困惑的是,如果我执行类似

$array = ['cat', 'dog', 'bird'];
end($array);
echo print_r($array, true);

['cat', 'dog', 'bird'] 的操作,就会打印出来。

我的理解是,通过引用传递某些内容会更改变量的值,因此我预计 $array 在传递到 end() 后仅打印出“Bird”。

我怀疑我对通过引用传递某些内容有一个根本性的误解......

I attempted to pass an explode statement into end() and got the following notice from my IDE, "Only variables can be passed by reference", which is not surprising.

However, what confuses me is that if I do something like

$array = ['cat', 'dog', 'bird'];
end($array);
echo print_r($array, true);

['cat', 'dog', 'bird'] prints out.

It is/was my understanding that passing something by references changes the value of the variable so I had expected $array to only print out 'Bird' after being passed into end().

I suspect I have a fundamental misunderstanding of passing something by reference...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

西瑶 2025-01-25 03:18:18

end()函数将内部指针移动到数组中的最后一个元素。该数组通过引用传递,因为它是由函数修改的。 这意味着您必须将其传递一个真实的变量,而不是返回数组的函数,因为只能通过参考传递实际变量。

end eend()更改参考阵列!
因此,您可以首先将值保存到另一个变量。实际上,它返回最后一个元素 false 用于空数组的值。
因此,您可以尝试:

$array = ['cat', 'dog', 'bird'];
$last = end($array);
echo print_r($last, true);

The end() function moves the internal pointer to, and outputs, the last element in the array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

end() function returns a value and does not change the reference array!
so you may save the value to another variable first. in fact it returns the value of the last element or false for empty array.
so you may try:

$array = ['cat', 'dog', 'bird'];
$last = end($array);
echo print_r($last, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文