%s(): Argument #%d%s%s%s cannot be passed by reference¶
Description¶
Passing a literal value, a constant or $GLOBALS
where a reference is requested leads to this error.
For literal and constants, global or class, this is due to the value that can’t be modified. It must be put in a variable first.
For $GLOBALS
, it is to prevent modifications of its values. It makes the error message a bit surprising. This doesn’t apply to other PHP variables.
A reference argument expected a variable, a property, static or not, or an array element. Usually, a variable is the best choice.
Example¶
<?php
function foo(&$arg) {}
// passing a literal
foo([]);
// passing $GLOBALS
foo($GLOBALS);
// trying to trim spaces on all GLOBALS
array_walk( $GLOBALS, trim(...));
?>
Literal Examples¶
array_walk(): Argument #1 $GLOBALS cannot be passed by reference
Solutions¶
Copy
$GLOBALS
to a variable and pass this variable.Copy the constant value to a variable and pass this variable.
Copy the literal value to a variable and pass this variable.