zoukankan      html  css  js  c++  java
  • PHP 中的闭包函数和匿名函数

    闭包函数

    闭包函数通常作为函数中的函数使用。

    <?php
    $foo = function($s) {
        echo $s;  
    };
    $foo('hello');
    
    <?php
    function test() {
        $a = 1;
        $b = 2;
        $foo = function($s) use($a, $b) {
            echo $s . ($a + $b);
        };
        $foo('hello');
    }
    test();
    
    <?php
    // 返回一个闭包函数供外部调用
    function test() {
        $foo = function($s) {
            echo $s;
        }; 
        return $foo;
    }
    $res = test();
    $res('hello')
    

    匿名函数

    匿名函数通常作为回调函数的参数使用。

    function foo($callback){
        return $callback();
    }
    
    $str1 = "hello";
    $str2 = "world";
    foo(function() use($str1, $str2) {  // 传入一个匿名函数作为参数
        echo $str1 . " " . $str2;
        return true;
    });
    
  • 相关阅读:
    EVM靶机渗透
    Joomla漏洞复现
    渗透测试
    Kali软件库认识
    谷歌hack语法
    Metasploit学习
    sqli-labs less-17
    sqli-labs(less-11-16)
    sqli-labs (less-8-less-10)
    less-7
  • 原文地址:https://www.cnblogs.com/danhuang/p/12916509.html
Copyright © 2011-2022 走看看