PHP匿名函數(shù)和use子句用法實(shí)例
本文實(shí)例講述了PHP匿名函數(shù)和use子句用法。分享給大家供大家參考,具體如下:
下面方法輸出的是hello world
$param1和$param2是閉包變量
function test() { $param2 = 'every'; // 返回一個(gè)匿名函數(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中多了一個(gè)引用
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日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP錯(cuò)誤提示It is not safe to rely on the system……的解決方法
今天小編就為大家分享一篇關(guān)于PHP錯(cuò)誤提示It is not safe to rely on the system……的解決方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03PHP實(shí)現(xiàn)自動(dòng)登入google play下載app report的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)自動(dòng)登入google play下載app report的方法,較為詳細(xì)的講述了登陸下載APP及對(duì)應(yīng)的實(shí)現(xiàn)代碼,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09

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

php模式設(shè)計(jì)之觀察者模式應(yīng)用實(shí)例分析