PHP匿名函數(shù)和use子句用法實例
本文實例講述了PHP匿名函數(shù)和use子句用法。分享給大家供大家參考,具體如下:
下面方法輸出的是hello world
$param1和$param2是閉包變量
function test() { $param2 = 'every'; // 返回一個匿名函數(shù) return function ($param1) use ($param2) { // use子句 讓匿名函數(shù)使用其作用域的變量 $param2 .= 'one'; print $param1 . ' ' . $param2; }; } $anonymous_func = test(); $anonymous_func('hello');
下面的方式 輸出hello everyone
function test() { $param2 = 'everyone'; $func = function ($param1) use ($param2) { // use子句 讓匿名函數(shù)使用其父作用域的變量 print $param1 . ' ' . $param2; }; $param2 = 'everybody'; return $func; } $anonymous_func = test(); $anonymous_func('hello');
下面的方式 輸出hello everybody
$param2中多了一個引用
function test() { $param2 = 'everyone'; $func = function ($param1) use (&$param2) { // use子句 讓匿名函數(shù)使用其父作用域的變量 print $param1 . ' ' . $param2; }; $param2 = 'everybody'; return $func; } $anonymous_func = test(); $anonymous_func('hello');
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP錯誤提示It is not safe to rely on the system……的解決方法
今天小編就為大家分享一篇關(guān)于PHP錯誤提示It is not safe to rely on the system……的解決方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03PHP實現(xiàn)自動登入google play下載app report的方法
這篇文章主要介紹了PHP實現(xiàn)自動登入google play下載app report的方法,較為詳細的講述了登陸下載APP及對應的實現(xiàn)代碼,具有不錯的實用價值,需要的朋友可以參考下2014-09-09

PHP number_format函數(shù)原理及實例解析