閉包函式
閉包函式通常作為函式中的函式使用,
<?php
$foo = function($s) {
echo $s;
};
$foo('hello');
<?php
function test() {
$a = 1;
$b = 2;
$foo = function($s) use($a, $b) {
echo $s . ($a + $b);
};
$foo('hello');
}
test();
<?php
// 回傳一個閉包函式供外部呼叫
function test() {
$foo = function($s) {
echo $s;
};
return $foo;
}
$res = test();
$res('hello')
匿名函式
匿名函式通常作為回呼函式的引數使用,
function foo($callback){
return $callback();
}
$str1 = "hello";
$str2 = "world";
foo(function() use($str1, $str2) { // 傳入一個匿名函式作為引數
echo $str1 . " " . $str2;
return true;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/38646.html
標籤:PHP
