How to fix “unexpected T_FUNCTION” error in PHP

- by

I tested one of my plugins yesterday and was shocked to see a nasty error message:

php-anonymous-function

What was going on? I could have sworn this worked fine last time I tried! Then I remembered that I changed my PHP Version in MAMP, from 5.5.3 back to 5.2.17. And exactly therein lies the problem, because the code that was amiss was something like this:

$zendash_updates = function ($a) {
	return null;
};

This is what’s known as an anonymous function in JavaScript, and it didn’t exist in PHP until Version 5.3.

If we wanted to be backward compatible, we can fix this by writing a function, then adding its output to the variable. It’s the same thing really, just a bit more code:

function zendash_update_helper($a) {
	return null;
};
$zendash_updates = zendash_update_helper();

Here’s a thread with an other example:



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.