In PHP, strict standards mode can issue warnings when passing non-variable values by reference. A common example of this is when using the array_shift() function.
The "Strict standards: Only variables should be passed by reference" warning appears when array_shift() is used with a non-variable value passed as an argument. For instance:
$instance = new MyClass(); $el = array_shift($instance->find(...)); // Warning
In contrast, when array_shift() is called with a variable containing an array, no warning is generated:
function get_arr() { return [1, 2]; } $el = array_shift(get_arr()); // No warning
The warning can be confusing because array_shift() is a function that returns an array value. However, under strict mode, PHP considers the return value of array_shift() as a non-variable.
To resolve the warning in strict mode, there are two options:
For example:
// Modify Method Signature function get_arr() { return [1, 2]; } $instance = new MyClass(); $el = array_shift($instance->get_arr()); // Use Intermediate Variable $el = array_shift($instance->get_arr() ?: []);
The above is the detailed content of Why Does PHP Issue 'Strict Standards: Only variables should be passed by reference' When Using array_shift()?. For more information, please follow other related articles on the PHP Chinese website!