## Can Anonymous Functions Be Executed Immediately in PHP?

Susan Sarandon
Release: 2024-10-25 02:11:02
Original
796 people have browsed it

## 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 */ })()
Copy after login

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>
Copy after login

However, in current PHP versions, you can directly execute anonymous functions:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!