smelikecat

导航

[php-error-report]PHP Strict Standards: Only variables should be passed by reference

// 报错代码:PHP Strict Standards:  Only variables should be passed by reference

$arr_userInfo['im_nation_cn'] = array_shift(preg_split('/,/' , $arr_userInfo['im_nation_cn']);

// 修改后,不再报错的代码,原因是 PHP 自带的函数 array_shift()是以引用的方式来传参,而引用传参,只能是一个变量
$arr_im_nation_cn = preg_split('/,/' , $arr_userInfo['im_nation_cn']);
$arr_userInfo['im_nation_cn'] =  array_shift($arr_im_nation_cn) ;

// Google 出来的原解答 , 原文链接:http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference
/*
Consider the following code:

error_reporting(E_STRICT);
class test {
    function test_arr(&$a) {
        var_dump($a);   
    }
    function get_arr() {
        return array(1,2);  
    }
}

$t= new test;
$t->test_arr($t->get_arr());
This will generate the following output:

Strict Standards: Only variables should be passed by reference in test.php on line 14
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
The reason? The test::get_arr() method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr() method returns an array value.

To get around this error in strict mode either change the signature of the method so it doesn't use a reference:

function test_arr($a) {
    var_dump($a);  
}
Since you can't change the signature of array_shift you can also use an intermediate variable:

$inter= get_arr();
$el= array_shift($inter);
*/

 

posted on 2016-02-25 17:31  smelikecat  阅读(273)  评论(0编辑  收藏  举报