PHP/Functions/func num args

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing function parameters without using the argument list

 
<?
function mean() {
    $sum = 0;
    $size = func_num_args();
    for ($i = 0; $i < $size; $i++) {
        $sum += func_get_arg($i);
    }
    $average = $sum / $size;
    return $average;
}
$mean = mean(96, 93, 97);
?>



Get parameter count from func_num_args ()

 
<?
function addNums() {
    $ret = "<table>";
    for ($x=0; $x<func_num_args (); $x++) {
        $arg = func_get_arg ($x);
        $result += $arg;
        $ret .= "<tr><td>number ". ($x+1).": </td><td>$arg</td></tr>";
    }
    $ret .= "<tr><td>result: </td><td>$result</td><tr>";
    $ret .= "</table>";
    return $ret;
}
print addNums (49, 60, 44, 22, 55);
?>



Variable Parameter Counts

 
function some_func($a, $b) {
            for ($i = 0; $i < func_num_args( ); ++$i) {
                    $param = func_get_arg($i);
                    echo "Received parameter $param.\n";
            }
    }
    function some_other_func($a, $b) {
            $param = func_get_args( );
            $param = join(", ", $param);
            echo "Received parameters: $param.\n";
    }
    some_func(1,2,3,4,5,6,7,8);
    some_other_func(1,2,3,4,5,6,7,8);