Home  >  Q&A  >  body text

How to properly handle expected parameter errors encountered

<p><br /></p> <pre class="brush:php;toolbar:false;">function calculate(){ echo "Number of parameters:" . func_get_args(); echo "The third parameter is:" . func_get_arg(3); print_r(func_get_arg()); $result = 0; foreach(func_get_arg() as $arg) : $result = $arg; end foreach; echo $result; calculate(10, 20, 30, 1502);</pre> <p>How to solve this problem, I have a bad argument error in the variadic argument list of this function? </p>
P粉976737101P粉976737101452 days ago508

reply all(1)I'll reply

  • P粉197639753

    P粉1976397532023-08-17 16:50:14

    Latest versions of PHP do not allow a mismatch between the function parameter list and the parameter list in the call. You can use ellipses in the argument list to allow unlimited arguments instead of calling func_get_args().

    function calculate(...$args){
        echo "参数个数:" . count($args) ;
        echo "第3个参数是:" . $args[3];
        print_r($args);
        $result = 0;
        foreach($args as $arg) :
            $result += $arg;
        end foreach;
        echo $result;
    }

    reply
    0
  • Cancelreply