PHP and Laravel knowledge points small accumulation

Source: Internet
Author: User

function () use ($x, &$y){}

Since PHP5.3 began to have the concept of Closure/anonymous functions, the USE keyword here is to allow anonymous functions to capture to the parent function scope

$x and $y variables exist within the Where &&y is a reference capture, that is, each time the anonymous function is called, the value of Y

The changes are also reflected here, while $x is a static reference.

<?PHP$message= "Hello\n";$example=function () {    Echo $message;};//notice:undefined variable:message$example();$example=function() Use($message) {    Echo $message;};//"Hello"$example();//inherited variable ' s value was from when the function was defined, not when called$message= "world\n";//"Hello"$example();//Inherit by-reference$message= "Hello\n";$example=function() Use(&$message) {    Echo $message;};//"Hello"$example();//The changed value in the parent scope was reflected inside the function call$message= "world\n";//"World"$example();//Closures can also accept regular arguments$example=function($arg) Use($message) {    Echo $arg. ‘ ‘ .$message;};//"Hello World"$example("Hello");

PHP and Laravel knowledge points small accumulation

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.