How to fix “unexpected T_FUNCTION” error in PHP

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:





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.