I want to create and execute a closure within a string in PHP and it’s not liking the way that I do it.
This code doesn’t work…
echo ( 'Hello, ' . (function($s) { return $s; })('World!') );
Yet, this is completely valid and works as intended…
$f = (function($s) { return $s; });
echo ( 'Hello, ' . $f('World!') );
Why won’t the first one work and is there a way to do it in one line (not because I think it’s efficient, because I’m sure it’s not)?
You may want to take a look at Self Executing functions in PHP5.3?.
Essentially, no self-invoking with “(…)()” until (maybe) sometime in 5.4.
https://wiki.php.net/rfc/fcallfcall