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 (with code)

烟雨青岚
Release: 2023-04-08 21:00:01
forward
2494 people have browsed it

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, &#39;timer&#39;);
Copy after login

2. Closure writing method

<pre class="brush:php;toolbar:false">
SwooleTimer::tick(2000, function () {
echo "hello world";
});
Copy after login

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, &#39;timer&#39;);
Copy after login

2. Closure writing method

<pre class="brush:php;toolbar:false">
$str = "hello world";
SwooleTimer::tick(2000, function () use ($str) {
echo $str;
});
Copy after login

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!

Related labels:
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template