Blogger Information
Blog 40
fans 0
comment 0
visits 37489
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP学习总结(6)匿名函数实例——2019年09月30号20:00分
虎子爸爸
Original
574 people have browsed it

上码:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>匿名函数的实例</title>
</head>
<style>
    .res{
        color:red;
        font-weight:700;
    }
</style>
<body>
    <h3>匿名函数的实例说明</h3>
    <ol>
        <li>
            <h4>命名函数</h4>
            <pre>
            function funName($a,$b){
                echo $a*$b;
            }
            funName(10,20);            
            </pre>
            <span>结果为:</span>
            <?php
            function funName($a,$b){
                echo $a*$b;
            }
            funName(10,20); 
            ?>
        </li>
        <li>
            <h4>匿名函数——以值的形式出现</h4>
            <pre>
            $result = function($a,$b){
                return $a*$b;
            };
            echo $result(10,20);
            </pre>
            <span>结果为:</span>
            <?php
            $result = function($a,$b){
                echo $a*$b;
            };
            $result(5,20);
            ?>
            <h4>注意——匿名函数只是以值的形式出现,但仍然是函数</h4>
            <b>$result(5,20)</b>
            
        </li>
        <li>
            <h4>匿名函数——以回调的形式出现</h4>
            <pre>
            先看回调函数——
            function backFun($a,$b){
                return $a*$b;
            }
            function baseFun($a,$b,$fun){
                if(!is_callable($fun)){
                    return false;
                }
                $res = $fun($a,$b);
                echo $a+$b+$res;
            }
            baseFun(10,20,'backFun');
            </pre>
            <span>结果为:</span>
            <span class="res">
            <?php
            function backFun($a,$b){
                return $a*$b;
            }
            function baseFun($a,$b,$fun){
                if(!is_callable($fun)){
                    return false;
                }
                $res = $fun($a,$b);
                echo $a+$b+$res;
            }
            baseFun(10,20,'backFun');
            ?>
            </span>
            <h4>现在把那个回调函数backFun变为匿名形式</h4>
            <pre>            
            baseFun(10,20,function($a,$b){
                $res = $a*$b;
                return $res;
            });
            </pre>
            <span>结果为:</span>
            <span class="res">
                <?php                
                baseFun(10,20,function($a,$b){
                    $res = $a*$b;
                    return $res;
                });

                ?>
            </span>
        </li>
        <li>
            <h4>匿名函数——以闭包的形式出现</h4>
            <pre>
            function closeFun($id){
                $name="怡红院";
                return function()use($name,$id){
                    return $name."今天共有".$id."位顾客";
                }
            }
            echo closeFun(10)();
            </pre>
            <span>结果为:</span>
            <span class="res">
                <?php
                function closeFun($id){
                    $name="怡红院";
                    return function()use($name,$id){
                        return $name."今天共有".$id."位顾客";
                    };
                }
                echo closeFun(10)();

                ?>
            </span>
        </li>
    </ol>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:

1、匿名函数以值的形式出现,还是函数;

2、匿名函数以回调的形式出现,主函数必须存在;

3、匿名函数以闭包的形式出现,需要注意2个地方,一是得有use,一是得2次()(),像这样closeFun()();

Correction status:qualified

Teacher's comments:匿名函数, 许多语言都有的, 通常也可以叫函数表达式,总是返回一个值
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post