PHP closure instance parsing

Source: Internet
Author: User
This article mainly introduces the PHP closure. if you need it, you can refer to the example in this article to analyze the concept machine usage of the closure in PHP program design and share it with you for your reference. The specific analysis is as follows:

In general, closures are PHP anonymous functions, but unlike functions, closures can use the value of the variable in the scope of the function declaration when using the function declaration.

The specific form is as follows:

$ A = function ($ arg1, $ arg2) use ($ variable) {// declare the function closure to the variable $ a. The parameter is $ arg1, $ arg2, the closure must use the $ variable}

The usage example is as follows:

<? Php $ result = 0; $ one = function () {var_dump ($ result) ;}; $ two = function () use ($ result) {var_dump ($ result) ;}; // The variable $ two itself records the declaration of the function and the value of the variable used by use $ three = function () use (& $ result) {var_dump ($ result) ;}; $ result ++; $ one (); // outputs NULL: $ result is not in scope $ two (); // outputs int (0): $ result was copied $ three (); // outputs int (1)?>

I hope this article will help you learn PHP programming.

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.