为什么PHP中的引用函数调用已弃用?

前端之家收集整理的这篇文章主要介绍了为什么PHP中的引用函数调用已弃用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了以下代码
  1. <?PHP
  2. $a1 = "WILLIAM";
  3. $a2 = "henry";
  4. $a3 = "gatES";
  5.  
  6. echo $a1." ".$a2." ".$a3. "<br />";
  7.  
  8. fix_names($a1,$a2,$a3);
  9.  
  10. echo $a1." ".$a2." ".$a3;
  11.  
  12. function fix_names(&$n1,&$n2,&$n3)
  13. {
  14. $a1 = ucfirst(strtolower(&$n1));
  15. $a2 = ucfirst(strtolower(&$n2));
  16. $a3 = ucfirst(strtolower(&$n3));
  17.  
  18. }
  19.  
  20. ?>

我收到了此通知:已弃用:已弃用调用时间传递引用

我需要解释为什么我会收到此通知?为什么在PHP版本5.3.13中,这已被弃用?

这些都记录在PHP Passing by Reference手册页上.具体(加重我的):

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);. And as of PHP 5.4.0,call-time pass-by-reference was
removed,so using it will raise a fatal error.

因此,它在PHP 5.3.x中被弃用(并将发出警告),并且在PHP 5.4中将失败.

这就是说,这是一个微不足道的修复.只需更新您的fix_names函数,如下所示:

  1. function fix_names(&$n1,&$n3)
  2. {
  3. $a1 = ucfirst(strtolower($n1));
  4. $a2 = ucfirst(strtolower($n2));
  5. $a3 = ucfirst(strtolower($n3));
  6.  
  7. }

顺便提一下,5.3.x系列的版本已经很长了,所以如果可能的话,更新到更新的版本(在进行必要的测试之后)是明智的.

猜你在找的PHP相关文章