How to properly handle expected parameter errors encountered
P粉976737101
P粉976737101 2023-08-16 17:36:54
0
1
523
<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粉976737101
P粉976737101

reply all(1)
P粉197639753

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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template