How do I immediately execute an anonymous function in PHP?

前端 未结 9 992
离开以前
离开以前 2020-11-27 03:29

In JavaScript, you can define anonymous functions that are executed immediately:

(function () { /* do something */ })()

Can you do somethin

9条回答
  •  借酒劲吻你
    2020-11-27 03:50

    This is the simplest for PHP 7.0 or later.

    php -r '(function() {echo 'Hi';})();'
    

    It means create closure, then call it as function by following "()". Works just like JS thanks to uniform variable evaluation order.

    https://3v4l.org/06EL3

提交回复
热议问题