Example of PHP anonymous function and use clause usage

Source: Internet
Author: User
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 ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.