Why is function call by reference in PHP deprecated?

后端 未结 3 2043
野的像风
野的像风 2021-01-04 12:28

I wrote the following code:

\"         


        
相关标签:
3条回答
  • 2021-01-04 12:39

    This is all documented on the PHP Passing by Reference manual page. Specifically (added emphasis mine):

    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.

    As such, it was deprecated (and will throw a warning) in PHP 5.3.x and will fail in PHP 5.4.

    That said, it's a trivial fix. Simply update your fix_names function as follows:

    function fix_names(&$n1, &$n2, &$n3)
    {
        $a1 = ucfirst(strtolower($n1));
        $a2 = ucfirst(strtolower($n2));
        $a3 = ucfirst(strtolower($n3));
    
    }
    

    Incidentally, the 5.3.x series is getting quite long in the tooth, so it would be wise to update to a more recent build (after carrying out the necessary testing) if at all possible.

    0 讨论(0)
  • 2021-01-04 12:41

    use this....

    fix_names(&$a1, &$a2, &$a3);

    0 讨论(0)
  • 2021-01-04 12:44

    Remove the & symbols from your like: &$n1, &$n2, ...

    0 讨论(0)
提交回复
热议问题