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:


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:

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:

Here’s a thread with an other example:

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Add your voice!