Q:

Write a PHP program to capture a variable number of arguments to a given function

0

Write a PHP program to capture a variable number of arguments to a given function

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
//Licence: https://bit.ly/2CFA5XY

function variadicFunction($operands)
{
    $sum = 0;
    foreach($operands as $singleOperand) {
        $sum += $singleOperand;
    }
    return $sum;
}

var_dump(variadicFunction([1, 2]));
var_dump(variadicFunction([1, 2, 3, 4]));
  
?>

Sample Output:

int(3)
int(10)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now