PHP anonymous function and use clause usage instance _php skill

Source: Internet
Author: User

This example describes the PHP anonymous function and use clause usage. Share to everyone for your reference, specific as follows:

The following method outputs the Hello World

$param 1 and $param2 are closed-pack variables

function test ()
{
  $param 2 = ' every ';
  Returns an anonymous function return function
  ($param 1) using ($param 2) {
    //USE clause let anonymous function take its scope variable
    $param 2. = ' one ';
    Print $param 1. ' ' . $param 2;}
  ;
}
$anonymous _func = Test ();
$anonymous _func (' hello ');

The following way output Hello everyone

function test ()
{
  $param 2 = ' everyone ';
  $func = function ($param 1) use ($param 2) {
    //using clause lets the anonymous function print $param 1 of the variable of its parent scope
    . ' ' . $param 2;
  };
  $param 2 = ' everybody ';
  return $func;
}
$anonymous _func = Test ();
$anonymous _func (' hello ');

The following way output Hello everybody

One more reference in $param 2

function test ()
{
  $param 2 = ' everyone ';
  $func = function ($param 1) Use (& $param 2)
    the {//Use clause lets the anonymous function print $param 1 of the variable of its parent scope
    . ' ' . $param 2;
  };
  $param 2 = ' everybody ';
  return $func;
}
$anonymous _func = Test ();
$anonymous _func (' hello ');

For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the PHP program design.

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.