"Only variables should be passed by reference" in PHP — What It Means and How to Fix It
What the PHP error "Only variables should be passed by reference" means and how to fix it.
Published November 1, 2026
A function that takes a parameter by reference (like end(), array_pop() or array_shift()) was given an expression instead of a variable.
What it means and why it happens
Functions needing a reference must receive a real variable so PHP can modify it. Passing the result of another function call fails.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
Notice: Only variables should be passed by reference
Practical examples
Code that triggers it
<?php
$last = end(explode(',', $csv)); // result of explode is not a variableThe fix
<?php
$parts = explode(',', $csv);
$last = end($parts);
// or PHP 7.3+: $last = array_key_last($parts) !== null ? $parts[array_key_last($parts)] : null;Use array helpers
$last = array_slice(explode(',', $csv), -1)[0];Common mistakes
- Passing function results straight to end() or reset().
- Ignoring the notice as harmless.
- Not storing the array first.
Key takeaways
- Only variables should be passed by reference: A function that takes a parameter by reference (like end(), array_pop() or array_shift()) was given an expression instead of a variable.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Which functions take references?
end, reset, array_pop, array_shift, sort and others.
How do I find which line caused Only variables should be passed by reference?
The error message and stack trace show the file and line; start there and work backwards.