PHP

How to demonstrate the use of variable arguments in PHP | PHP programs

How to demonstrate the use of variable arguments in PHP | PHP programs

How to demonstrate the use of variable arguments in PHP | PHP programs

We will calculate the addition of arguments, here we will use a variable number of arguments in the user to define a function using ... (three dots). Here, we can pass any number of arguments to the function.

<?php

function Sum(...$values)
{
    $res = 0;
    foreach ($values as $val)
    {
        $res = $res + $val;
    }
    return $res;
}

$sum = Sum(10, 20);
echo "Sum: " . $sum . "<br>";

$sum = Sum(10, 20, 30);
echo "Sum: " . $sum . "<br>";
?>
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community