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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the "Only Variables Should Be Passed By Reference" Warning in PHP

You encounter the strict standards error Strict standards: Only variables should be passed by reference when executing code like $el = array_shift($instance->find(..)). However, if you swap that dynamic call with a simple function returning an array, such as $el = array_shift(get_arr()), the warning disappears. This discrepancy often leads to confusion about whether array_shift actually requires a variable reference or if PHP's strict mode is behaving inconsistently.

The root cause lies in how PHP resolves function arguments at runtime versus compile time, specifically regarding implicit variable references within the stack. While array_shift does not explicitly require its argument to be passed by reference (you do not need $ref = &$arr; array_shift($ref);), it internally modifies the array it receives. In modern PHP versions running strict standards, this internal modification triggers a check on the type of the passed parameter in specific contexts involving dynamic property access.

When you call $instance->find(..), the result is not a static value known at parse time; it is a runtime evaluation that returns an object or array depending on your implementation. In PHP 8 and later, strict typing rules have tightened regarding how references are handled implicitly. When array_shift receives an expression that resolves to an array but isn't explicitly a variable in the stack frame of the current scope, certain internal optimizations or checks might flag it if the engine detects a potential modification pattern that conflicts with static analysis expectations.

The code $el = array_shift(get_arr()) works because get_arr() is treated as a pure function call returning a value. The reference handling inside array_shift is satisfied differently when the argument originates from a distinct function stack frame versus a property accessor. Conversely, $instance->find(..) might be resolving to an object property or method that returns the array directly within the same scope context, causing the engine's strict mode checker to interpret the implicit reference passing as a violation of variable usage rules in strict environments.

To resolve this without rewriting your logic, you can explicitly capture the result into a local variable before passing it to array_shift. For instance:

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

This pattern forces the engine to treat the argument as a standard variable reference, satisfying the strict standards check. Alternatively, you can disable strict error reporting for this specific line if performance profiling shows no overhead from the warning suppression, though disabling E_STRICT globally is rarely recommended for production codebases.

Understanding when PHP flags implicit references helps prevent subtle bugs in type-heavy applications. If you are using Laravel or a similar framework, ensure your service providers or models returning collections adhere to these reference expectations. While this specific warning usually does not alter the runtime behavior of array_shift, ignoring it can indicate underlying issues with how data flows through your application's strict typing boundaries.

For deeper insights into PHP's internal handling of references and strict standards, refer to official documentation on PHP Reference or check LaravelCompany.com for best practices on managing strict mode in modern PHP applications.

Our network