[wp-forums] PHP 5.4 pass-by-reference errors

Otto otto at ottodestruct.com
Tue Dec 24 20:15:38 UTC 2013


You're probably going to start seeing some of this in the forums. I
saw a couple this morning.

With some hosts going to PHP 5.4, a fatal error is going to start
cropping up in older code. It's the call-time pass-by-reference error.
It should look like this:

PHP Fatal error: Call-time pass-by-reference has been removed in ... on line 123

This occurs when there is a function call that looks like the following:

someFunctionCall( &$var );

The & ampersand symbol is not valid in a function call anymore. It has
been deprecated since PHP 5.0, but now in 5.4 it causes a fatal error.
In 5.3 it only caused a warning which would be ignored most of the
time.

The general fix to simply remove the & ampersand. This usually solves
it. In a theme, this is almost always the only thing you have to do.

In some rare cases, this may cause a deeper underlying problem if the
code really needed that variable to be passed by reference. In which
case, the function definition would need to be modified as well.

It's like this:

example( &$var );
function example( $var) {
...
}

Would become:

example( $var );
function example( &$var ) {
...
}


However, this is only needed with pass-by-reference is necessary,
which it is usually not.

More details:
http://php.net/manual/en/language.references.pass.php

So, if you see somebody complaining about 5.4 incompatibility, get the
error message from them, tell them to look at that line, and remove
the & ampersand.

-Otto


More information about the wp-forums mailing list