Log in

View Full Version : Resolved PHP version change, using anonymous functions



djr33
02-21-2014, 03:38 AM
I have some old code that isn't working:

function($str) { return '.'.$str; }
Context:

array_map((function($str) { return '.'.$str; }),$array))

My server is currently running PHP 5.2.17. The old one was probably version 4. I don't remember, and it's been a while.

I can certainly work around this on my own, but I'm wondering if there's a proper/recommended method to fix this in general. It comes up once in a while.


Edit: Ah. I must have gone BACK a version. It was probably 5.3 before, now 5.2, without anonymous functions. It looks like create_function() is my best option?
I should get my host to update PHP I guess....

traq
02-21-2014, 04:35 AM
create_function is your best bet, yes, but there are problematic things about it. I would definitely go with the upgrade-php-and-use-real-anonymous-functions approach.

djr33
02-21-2014, 04:49 AM
Alright, I was able to update the version of PHP on the server. Resolved.

Thanks-- I was also wondering about the best answer, so that works (if necessary).