One step to get it done, the usage and difference of use in the closure function in PHP, as well as the meaning of & reference will be explained in detail for you

亚连
Release: 2023-03-26 10:28:01
Original
1902 people have browsed it

use means to connect the closure and the external variable. Using reference & and not using reference means assigning value when calling or assigning value when declaring. The difference is that assigning value when calling will be due to the reference variable Change and get the latest value. The value assigned when declaring is the value of the latest variable when used.

$result = 0;

$one = function()
{ var_dump($result); };

$two = function() use ($result)
{ var_dump($result); };

$three = function() use (&$result)
{ var_dump($result); };

$fore = function($result)
{ var_dump($result); };

$result++;$one();    // outputs NULL: $result is not in scope
$two();    // outputs int(0): $result was copied
$three();    // outputs int(1)
$fore($result);    // outputs int(1)exit;
Copy after login

The above is the usage method and difference of use in the closure function in php. I hope it will be useful to everyone in the future. helpful.

Related articles:

Detailed explanation of PHP class and method keyword tutorial

PHP closure function() use( )

Detailed usage guide for PHP namespace namespace and import use

The above is the detailed content of One step to get it done, the usage and difference of use in the closure function in PHP, as well as the meaning of & reference will be explained in detail for you. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
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!