Home  >  Article  >  Backend Development  >  ## How to Execute Anonymous Functions Instantly in PHP?

## How to Execute Anonymous Functions Instantly in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-10-25 02:16:02312browse

## How to Execute Anonymous Functions Instantly in PHP?

Executing Anonymous Functions Instantly in PHP

In JavaScript, defining and executing anonymous functions immediately is straightforward using the syntax:

<code class="js">(function () { /* do something */ })()</code>

Is there a similar mechanism in PHP?

PHP 7

With PHP versions 7 and later, anonymous functions can be executed instantly and conveniently:

<code class="php">(function() { echo 'executed'; })();</code>

Pre-PHP 7

Prior to PHP 7, the only way to achieve immediate execution was through the call_user_func() function:

<code class="php">call_user_func(function() { echo 'executed'; });</code>

The above is the detailed content of ## How to Execute Anonymous Functions Instantly 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