This article mainly introduces the usage of anonymous functions and use clauses in PHP, and analyzes the usage skills of anonymous functions and use clauses in combination with examples, for more information about anonymous PHP functions and use clauses, see the example in this article. We will share this with you for your reference. The details are as follows:
The following method outputs hello world
$ Param1 and $ param2 are closure variables.
Function test () {$ param2 = 'every'; // returns an anonymous function return function ($ param1) use ($ param2) {// use clause allows anonymous functions to use the variable $ param2 in its scope. = 'one'; print $ param1. ''. $ param2 ;}}$ anonymous_func = test (); $ anonymous_func ('Hello ');
The following method outputs hello everyone
Function test () {$ param2 = 'everone'; $ func = function ($ param1) use ($ param2) {// use clause allows anonymous functions to use the print $ param1 variable of its parent scope. ''. $ param2 ;}; $ param2 = 'everbody'; return $ func ;}$ anonymous_func = test (); $ anonymous_func ('Hello ');
The following method outputs hello everybody
$ Param2 has one more reference
Function test () {$ param2 = 'everone'; $ func = function ($ param1) use (& $ param2) {// use clause allows anonymous functions to use the print $ param1 variable of its parent scope. ''. $ param2 ;}; $ param2 = 'everbody'; return $ func ;}$ anonymous_func = test (); $ anonymous_func ('Hello ');