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;
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
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!