You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
<?php
|
|
/**
|
|
* 函数内部的无名函数
|
|
* 与普通函数一样有返回值
|
|
* 可以作为一个变量值使用
|
|
* 常用于做回调函数的参数
|
|
*/
|
|
|
|
//1 匿名函数作为变量值
|
|
$a = function(){
|
|
echo 'hello'.PHP_EOL;
|
|
return "这是匿名函数的返回值";
|
|
};
|
|
|
|
$a();
|
|
echo $a().PHP_EOL; //output:会执行函数里的代码,并且返回返回值
|
|
|
|
//2 匿名函数的参数
|
|
$b=function($var){
|
|
echo $var.PHP_EOL;
|
|
return "返回".$var;
|
|
};
|
|
|
|
$b("匿名函数的参数");
|
|
echo $b("匿名函数的参数").PHP_EOL;
|
|
|
|
|
|
//2 匿名函数作为函数里的函数
|
|
|
|
function func(){
|
|
$b='world';
|
|
|
|
$innerFunc = function(){
|
|
//调用函数func的局部变量
|
|
echo $b; //wrong output:Undefined variable: b
|
|
};
|
|
|
|
//调用匿名函数
|
|
$innerFunc();
|
|
}
|
|
|
|
|
|
// //匿名函数作为参数,回调函数
|
|
function mathOpt(function(){return 0;}){
|
|
|
|
}
|
|
?>
|