Only variables can be passed by reference

前端 未结 8 1222
小鲜肉
小鲜肉 2020-12-11 00:59

I had the bright idea of using a custom error handler which led me down a rabbit hole.

Following code gives (with and without custom error handler): Fatal er

相关标签:
8条回答
  • 2020-12-11 01:29

    Here is what I get when trying your second php code snippet in php-cli after setting error_reporting to E_ALL | E_STRICT

        gparis@techosaure:~/workspace/universcine.com$ php -a
    Interactive shell
    
    php > function foo(){
    php {     $a=explode( '/' , 'a/b/c');
    php {     $c=array_pop(array_slice($a,-2,1));
    php {     return $c;
    php { }
    php > print_r(foo());
    PHP Strict standards:  Only variables should be passed by reference in php shell code on line 3
    PHP Stack trace:
    PHP   1. {main}() php shell code:0
    PHP   2. foo() php shell code:1
    

    As you can see, it's only strict standards here. And you can easily let your custom error handler ignore them (based on the value you get : 2048 for instance, here).

    As of php 5.3, E_ALL does not include E_STRICT, look at this :

    php > foreach(array("E_ALL", "E_DEPRECATED", "E_STRICT", "E_NOTICE", "E_PARSE", "E_WARNING") as $const) echo $const . "  :\t" . constant($const) ."\t". decbin(constant($const)). "\n";
    E_ALL  :        30719   111011111111111
    E_DEPRECATED  : 8192     10000000000000
    E_STRICT  :     2048       100000000000
    E_NOTICE  :     8                  1000
    E_PARSE  :      4                   100
    E_WARNING  :    2                    10
    

    As of php 5.4, E_ALL does include E_STRICT :

    E_ALL  :            32767   111111111111111
    E_DEPRECATED  :     8192     10000000000000
    E_STRICT  :         2048       100000000000
    E_NOTICE  :         8                  1000
    E_PARSE  :          4                   100
    E_WARNING  :        2                    10
    
    0 讨论(0)
  • 2020-12-11 01:33

    I think that now (since php 5) it should be:

    function &foo(){ //NOTICE THE &
        $b=array_pop(array("a","b","c"));
        return $b;
    }
    print_r(foo());
    

    and

    function &foo(){ //NOTICE THE &
        $a=explode( '/' , 'a/b/c');
        $c=array_pop(array_slice($a, $b = -2, $c = 1)); //NOW NO DIRECT VALUES ARE PASSED IT MUST BE VARIABLES
        return $c;
    }
    print_r(foo());
    

    but i'm just a begginer :)

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