java - What is the difference between using global directly and using use to pass variables in external space in PHP anonymous functions?

WBOY
Release: 2016-08-18 09:16:30
Original
977 people have browsed it

What is the difference between using global directly in PHP’s anonymous function and using use to pass variables in external space?

Reply content:

What is the difference between using global directly in PHP’s anonymous function and using use to pass variables in external space?

The variables of the closure function use will not affect global variables.

<code><?php
$global = $global2= 'hello';
$fun = function() use ($global){
    $global.=' world!';
    global $global2;
    $global2.=' world!';
};
$fun();
echo $global."\n";
//hello
echo $global2;
//hello world!
</code>
Copy after login
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!