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

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

WBOY
WBOYOriginal
2016-08-18 09:16:301062browse

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>
Statement:
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