Home > Backend Development > PHP Tutorial > 关于php闭包附加状态的问题

关于php闭包附加状态的问题

WBOY
Release: 2016-06-06 20:10:50
Original
1035 people have browsed it

直接上代码:

<code><?php function enclosePerson($name){
        return function($doCommand) use ($name) {
                return sprintf('%s, %s', $name, $doCommand);
        }
}
$func = enclosePerson("Simon");

echo $func("you are a cool man!");
?>
</code>
Copy after login
Copy after login

理论应该打印:Simon, you are a cool man!

但实际打印为空!

什么问题造成的呢?

php version:5.5.29

回复内容:

直接上代码:

<code><?php function enclosePerson($name){
        return function($doCommand) use ($name) {
                return sprintf('%s, %s', $name, $doCommand);
        }
}
$func = enclosePerson("Simon");

echo $func("you are a cool man!");
?>
</code>
Copy after login
Copy after login

理论应该打印:Simon, you are a cool man!

但实际打印为空!

什么问题造成的呢?

php version:5.5.29

因为你语法错误
return 后面少了个 ;

<code>return function($doCommand) use ($name) {
                return sprintf('%s, %s', $name, $doCommand);
        };</code>
Copy after login

晕,LZ是在web页面中写的吧? 一片空白这种问题很明显是出错了。而且是致命性的错误。看看错误日志就知道了。

从我这边看到的错误是:

<code>PHP Parse error:  syntax error, unexpected '}' in - on line 6</code>
Copy after login
Related labels:
php
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