In PHP, the strict standards warning "Only variables should be passed by reference" triggers when an attempt is made to pass a non-variable by reference to a function or method.
Consider the following code:
$el = array_shift($instance->find());
This code generates a strict standards warning because $instance->find() returns an array, which is not a variable. When passed as an argument, it attempts to pass the array by reference (using the & operator), whichtriggers the warning.
However, in the following code:
function get_arr(){ return array(1, 2); } $el = array_shift(get_arr());
The strict standards warning is not reported because get_arr() is a function that returns an array. Therefore, it is treated as a variable rather than just an array value.
The strict standards warning occurs specifically in situations where a method or function is called and its return value is passed by reference. For instance, consider the following code:
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());
In this code, the strict standards warning is generated because $t->get_arr() returns an array, which is not a variable. However, it is passed by reference to the test_arr method using the & operator.
To resolve the strict standards warning, there are two possible approaches:
function test_arr($a) { var_dump($a); }
$inter = $instance->find(); $el = array_shift($inter);
The above is the detailed content of Why Does PHP Issue 'Only Variables Should Be Passed by Reference' Warnings, and How Can They Be Resolved?. For more information, please follow other related articles on the PHP Chinese website!