$str="hello world";functiontimer(){global$str;echo$str;}SwooleTimer::tick(2000,'timer' );"."/> $str="hello world";functiontimer(){global$str;echo$str;}SwooleTimer::tick(2000,'timer' );".">
Home >Backend Development >PHP Tutorial >Several ways to write anonymous functions in PHP that people don't know (with code)
Several ways to write anonymous functions in PHP that people don’t know
Common writing methods:
1. Traditional writing method
<pre class="brush:php;toolbar:false"> function timer () { echo "hello world"; } SwooleTimer::tick(2000, 'timer');
2. Closure writing method
<pre class="brush:php;toolbar:false"> SwooleTimer::tick(2000, function () { echo "hello world"; });
Advanced writing method:
1. Traditional writing method
<pre class="brush:php;toolbar:false"> $str = "hello world"; function timer () { global $str; echo $str; } SwooleTimer::tick(2000, 'timer');
2. Closure writing method
<pre class="brush:php;toolbar:false"> $str = "hello world"; SwooleTimer::tick(2000, function () use ($str) { echo $str; });
Thank you all for reading, I hope you will benefit a lot.
This article is reproduced from: https://www.cnblogs.com/newmiracle/p/11875325.html
Recommended tutorial: "php tutorial"
The above is the detailed content of Several ways to write anonymous functions in PHP that people don't know (with code). For more information, please follow other related articles on the PHP Chinese website!