gpt4 book ai didi

php - 调用时通过引用警告

转载 作者:行者123 更新时间:2023-12-04 00:06:13 24 4
gpt4 key购买 nike

    function heaviside(&$value, $key, &$array)
{
if($key > 0)
$value = $array[$key-1].$array[$key];
}

function test_heaviside()
{
for($i=0; $i<10; $i++)
{
$array[$i] = $i;
}
array_walk($array, 'heaviside', &$array);
print_r($array);
}

test_heaviside();

我的问题是上面的代码会生成此警告:

PHP Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of array_walk(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer.

如果我在调用 array_walk 时删除 &$array 中的 &,该函数将不会返回正确的结果。在第一种情况下,它会返回以下结果:

[0] => 0
[1] => 01
[2] => 012
[3] => 0123
[4] => 01234
[5] => 012345
[6] => 0123456
[7] => 01234567
[8] => 012345678
[9] => 0123456789

如果我删除它并返回:

[0] => 0
[1] => 01
[2] => 12
[3] => 23
[4] => 34
[5] => 45
[6] => 56
[7] => 67
[8] => 78
[9] => 89

我需要帮助理解这一点,或者只是为了找到除更改 .ini 之外的解决方案。

最佳答案

来自php manual about references

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.

这意味着您无法将参数视为引用。只有函数定义可以做到这一点。如果你看array_walk manual page你会看到只有第一个参数是引用,最后一个不能是。

所以最终,你想要的东西被弃用了。您可以

  • 更改ini(您不想更改)
  • 降级您的 php 版本
  • 通过设置较低的错误级别来消除警告。
  • 编写您自己的 array_walk 函数,该函数将引用作为第三个参数

关于php - 调用时通过引用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5177802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com