Home > Backend Development > PHP Tutorial > Why Does `array_shift()` Trigger 'Strict Standards: Only Variables Should Be Passed by Reference'?

Why Does `array_shift()` Trigger 'Strict Standards: Only Variables Should Be Passed by Reference'?

Susan Sarandon
Release: 2025-01-03 17:27:43
Original
883 people have browsed it

Why Does `array_shift()` Trigger

Error Message "Strict Standards: Only Variables Should Be Passed by Reference"

When using array_shift(), it may report a strict standards warning if the argument passed is the result of a function call. This behavior is seemingly inconsistent, as it does not always trigger the warning.

Consider the following code:

$el = array_shift($instance->find(..))
Copy after login

In this example, the warning is raised because the find() method is not a variable. However, the following code does not produce a warning:

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

To understand this behavior, let's analyze a different code snippet:

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());
Copy after login

This code generates a strict standards warning because the $t->get_arr() method is not a variable and is being passed by reference. This behavior is counterintuitive because the method returns an array value.

To avoid this error in strict mode, either change the method signature to avoid using a reference or use an intermediate variable:

function test_arr($a) {
    var_dump($a);
}

$inter = get_arr();
$el = array_shift($inter);
Copy after login

The above is the detailed content of Why Does `array_shift()` Trigger 'Strict Standards: Only Variables Should Be Passed by Reference'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template