国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP閉包定義與使用簡單示例

2024-05-05 00:03:13
字體:
來源:轉載
供稿:網友

本文實例講述了PHP閉包定義與使用。分享給大家供大家參考,具體如下:

<?phpfunction getClosure($i){  $i = $i.'-'.date('H:i:s');  return function ($param) use ($i) {    echo "--- param: $param ---/n";    echo "--- i: $i ---/n";  };}$c = getClosure(123);$i = 456;$c('test');sleep(3);$c2 = getClosure(123);$c2('test');$c('test');/*output:--- param: test ------ i: 123-21:36:52 ------ param: test ------ i: 123-21:36:55 ------ param: test ------ i: 123-21:36:52 ---*/

再來一個實例

$message = 'hello';$example = function() use ($message){ var_dump($message);};echo $example();//輸出hello$message = 'world';//輸出hello 因為繼承變量的值的時候是函數定義的時候而不是 函數被調用的時候echo $example();//重置為hello$message = 'hello';//此處傳引用$example = function() use(&$message){ var_dump($message);};echo $example();//輸出hello$message = 'world';echo $example();//此處輸出world//閉包函數也用于正常的傳值$message = 'hello';$example = function ($data) use ($message){ return "{$data},{$message}";};echo $example('world');//此處輸出world,hello

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 封丘县| 张掖市| 涪陵区| 四川省| 金湖县| 株洲市| 和顺县| 泽普县| 科尔| 迁安市| 崇礼县| 耒阳市| 邹平县| 灯塔市| 通海县| 九寨沟县| 昌图县| 彭州市| 芷江| 仁寿县| 尼玛县| 安阳县| 洱源县| 阿合奇县| 嘉兴市| 抚顺县| 栖霞市| 渝北区| 青铜峡市| 酒泉市| 贺州市| 南宁市| 蒲江县| 方正县| 积石山| 乌拉特后旗| 祁门县| 曲阜市| 金门县| 巴塘县| 彭水|