Home  >  Article  >  Backend Development  >  ## Can Anonymous Functions Be Executed Immediately in PHP?

## Can Anonymous Functions Be Executed Immediately in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-25 02:11:02797browse

## Can Anonymous Functions Be Executed Immediately in PHP?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn