Home >Backend Development >PHP Tutorial >Summary of techniques for using anonymous functions in PHP (with code)

Summary of techniques for using anonymous functions in PHP (with code)

不言
不言forward
2018-11-17 16:53:452721browse

This article brings you a summary of the techniques for using anonymous functions in PHP (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Anonymous functions in php, also called closure functions, allow you to specify a function without a name. Assign the anonymous function to a variable and call it through the variable. A simple example:

<?php
$anonymousFunc = function($username){
    echo $username;
  };
  $anonymousFunc("乔峰!");

Tip 1: Place the anonymous function In ordinary functions, anonymous functions can also be returned, thus forming a simple closure

<?php
function closureFunc(){
    $anonymousFunc = function(){
        echo "乔峰!";
    };
    $anonymousFunc();//普通函数内部调用了匿名函数
}
closureFunc();//输出: 乔峰

Tip 2 In Reference local variables in anonymous functions (here you need to quote a PHP keyword use)

<?php
function closureFunc(){
    $username = &#39;乔峰&#39;;
    $anonymousFunc = function() use($username){
        echo $username;
    };
    $anonymousFunc();//此处调用了匿名函数
}
closureFunc();//输出: 乔峰

Tips 3 Return anonymous in ordinary functions Function

<?php
function closureFunc(){
    $username = &#39;乔峰&#39;;
    $anonymousFunc = function() use($username){
        echo $username;
    };
    return $anonymousFunc;// 函数返回匿名函数
}
$func = closureFunc();
$func(); //然后调用$func()

Tip 4 Return an anonymous function and pass parameters to the anonymous function

<?php
function closureFunc(){
    $username = &#39;乔峰&#39;;
    $anonymousFunc = function($lover,$skill) use($username){
        echo $username.$lover.$skill;
    };
    return $anonymousFunc;
}
$func = closureFunc();
$func("阿朱","擒龙手");//乔峰阿朱擒龙手

Tip 5 Use closure to change the variable value referenced by the context

<?php

function closureFunc(){
    $number = 100;
    $anonymousFunc = function() use($number) {
        $number++;
        echo $number.PHP_EOL;
    };
    echo $number.PHP_EOL;
    return $anonymousFunc;
}
$func = closureFunc();// 这里输出1,直接调用本函数的 echo $number.PHP_EOL; 即为100
$func();// 调用函数的返回值 $anonymousFunc  $number++ 即为101
$func(); //101
$func();//101

Based on the above input results, it is found that the following two func() are both Returns 101, the value has not changed. If you want to accumulate the effect, just add an & reference symbol (modifications within the anonymous function will also affect external variables), modify it:

<?php

function closureFunc(){
    $number = 100;
    $anonymousFunc = function() use(&$number) {
        $number++;
        echo $number.PHP_EOL;
    };
    echo $number.PHP_EOL;
    return $anonymousFunc;
}
$func = closureFunc();// 这里输出1,直接调用本函数的 echo $number.PHP_EOL; 即为100
$func();// 调用函数的返回值 $anonymousFunc  $number++ 即为101
$func(); //102
$func();//103

Tips 6 Pass anonymous functions as parameters

<?php
//定义普通函数,anonymousFunc 为参数变量
function myFunc($anonymousFunc){
    $anonymousFunc("乔峰");
}
myFunc(function($username){ //这里调用普通函数,并把 匿名函数作为参数 传给了myFunc中的$anonymousFunc
    echo $username;
});//输出 乔峰

The above is the detailed content of Summary of techniques for using anonymous functions in PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:what does bug meanNext article:what does bug mean