Home > Backend Development > PHP Tutorial > Why Does PHP Issue 'Only Variables Should Be Passed by Reference' Warnings, and How Can They Be Resolved?

Why Does PHP Issue 'Only Variables Should Be Passed by Reference' Warnings, and How Can They Be Resolved?

Mary-Kate Olsen
Release: 2025-01-03 22:46:42
Original
676 people have browsed it

Why Does PHP Issue

Understanding the Strict Standards Warning: "Only Variables Should Be Passed by Reference"

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.

Explanation of the Behavior

Consider the following code:

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

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

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 Root Cause

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

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.

Resolution

To resolve the strict standards warning, there are two possible approaches:

  1. Modify Method Signature: Change the method's signature to not use a reference parameter. For example:
function test_arr($a) {
    var_dump($a);
}
Copy after login
  1. Use an Intermediate Variable: Create an intermediate variable, assign the method's return value to it, and pass the intermediate variable by reference. For example:
$inter = $instance->find();
$el = array_shift($inter);
Copy after login

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!

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