Example of PHP anonymous function and use clause usage, and example of anonymous use clause. Example of PHP anonymous function and use clause usage. example of anonymous use clause this article describes the usage of PHP anonymous function and use clause. For your reference, refer to the following method: example of PHP anonymous function and use clause usage, and example of anonymous use clause.
This example describes the usage of PHP anonymous functions and use clauses. 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 ');