Error message "Strict standards: Only variables should be passed by reference" Error message "Strict standards: Only variables should be passed by reference" php php

Error message "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 14array(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);


$instance->find() returns a reference to a variable.

You get the report when you are trying to use this reference as an argument to a function, without storing it in a variable first.

This helps preventing memory leaks and will probably become an error in the next PHP versions.

Your second code block would throw an error if it wrote like (note the & in the function signature):

function &get_arr(){    return array(1, 2);}$el = array_shift(get_arr());

So a quick (and not so nice) fix would be:

$el = array_shift($tmp = $instance->find(..));

Basically, you do an assignment to a temporary variable first and send the variable as an argument.


The cause of the error is the use of the internal PHP programming data structures function, array_shift() [php.net/end].

The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift() in the manual", there isn't any cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.

Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.

Reproduce code:

function get_arr(){    return array(1, 2);}$array = get_arr();$el = array_shift($array);