Instant Execution of Anonymous Functions in PHP
Question:
In JavaScript, anonymous functions can be defined and executed immediately:
(function () { /* do something */ })()
Is there a similar approach available in PHP?
Answer:
Prior to PHP 7, executing anonymous functions immediately could be achieved using call_user_func():
<code class="php">call_user_func(function() { echo 'executed'; });</code>
However, in current PHP versions, you can directly execute anonymous functions:
<code class="php">(function() { echo 'executed'; })();</code>
The above is the detailed content of ## Can Anonymous Functions Be Executed Immediately in PHP?. For more information, please follow other related articles on the PHP Chinese website!