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)

不言
Release: 2023-04-04 12:24:01
forward
2737 people have browsed it

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("乔峰!");
Copy after login

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();//输出: 乔峰
Copy after login

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();//输出: 乔峰
Copy after login

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()
Copy after login

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("阿朱","擒龙手");//乔峰阿朱擒龙手
Copy after login

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
Copy after login

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
Copy after login

Tips 6 Pass anonymous functions as parameters

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

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!

Related labels:
source:cnblogs.com
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