Blogger Information
Blog 28
fans 0
comment 0
visits 19700
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0928 乘积函数 20190928 2000-2200
没有人的夏天的博客
Original
551 people have browsed it

剩余参数的应用


在实参中使用剩余参数...标识,可以 将实际参数内容扩展到形参的一维数组中, 如果不使用剩余参数,则将实际参数添加到形参的二维数组中.


剩余参数在乘法计算中的应用

在本案例中,输入的字符串需要进行格式判断和转换,只识别* 进行乘法计算

php代码与html代码注意空格的衔接


效果图

  TIM截图20191005150035.png

实例

<!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>Document</title>
</head>

<body>

    <form action="" method="get">
        <label for="num">请输入计算数字</label>
        <input type="text" id="num" name="num" value="<?php echo $_GET['num']; ?>">
        <button>计算</button>
        <?php
        //判断参数是否为空 ,
        if (empty($_GET['num'])) {
            die("请输入正确参数");
        }
        // 转换字符串为数组,本案例只识别*
        $_GET = explode('*', $_GET['num']);
        // 数组传参
        echo sum(...$_GET);

        ?>
    </form>
    <?php
    // 接受数组参数进行乘法计算
    function sum(...$parameter)
    {
        $total = 1;
        foreach ($parameter as $v) {
            $total *= $v;
        }
        return $total;
    }
    ?>
</body>

</html>

运行实例 »

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






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