Home > Backend Development > PHP Tutorial > PHP anonymous function and use clause usage examples, anonymous use clause examples_PHP tutorial

PHP anonymous function and use clause usage examples, anonymous use clause examples_PHP tutorial

WBOY
Release: 2016-07-12 08:56:52
Original
1092 people have browsed it

PHP anonymous function and use clause usage examples, anonymous use clause examples

The examples in this article describe the usage of PHP anonymous functions and use clauses. Share it with everyone for your reference, the details are as follows:

The following method outputs hello world

$param1 and $param2 are closure variables

function test()
{
  $param2 = 'every';
  // 返回一个匿名函数
  return function ($param1) use ($param2) {
    // use子句 让匿名函数使用其作用域的变量
    $param2 .= 'one';
    print $param1 . ' ' . $param2;
  };
}
$anonymous_func = test();
$anonymous_func('hello');

Copy after login

The following method outputs hello everyone

function test()
{
  $param2 = 'everyone';
  $func = function ($param1) use ($param2) {
    // use子句 让匿名函数使用其父作用域的变量
    print $param1 . ' ' . $param2;
  };
  $param2 = 'everybody';
  return $func;
}
$anonymous_func = test();
$anonymous_func('hello');

Copy after login

The following method outputs hello everybody

There is one more reference in $param2

function test()
{
  $param2 = 'everyone';
  $func = function ($param1) use (&$param2) {
    // use子句 让匿名函数使用其父作用域的变量
    print $param1 . ' ' . $param2;
  };
  $param2 = 'everybody';
  return $func;
}
$anonymous_func = test();
$anonymous_func('hello');

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "php-oriented "Introduction Tutorial on Object Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • A preliminary study on PHP’s closure (Closure) anonymous function
  • Detailed explanation of PHP’s closure (Closure) anonymous function
  • A brief analysis of closures (anonymous functions) in PHP
  • Usage examples of debug_backtrace, debug_print_backtrace and anonymous functions in PHP
  • Examples of using anonymous functions to operate databases in PHP
  • php Analysis of namespace use usage examples
  • Overview of use keyword in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1111341.htmlTechArticlePHP anonymous function and use clause usage examples, anonymous use clause examples This article describes PHP anonymous functions and use Clause usage. Share it with everyone for your reference, the details are as follows: The following method...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template