Monday, September 4, 2017

PHP Variable Length Argument Function


PHP 5.6 introduced variable length argument function. In which we can pass 0, 1 or n number of arguments in function. To do so, you need to use 3 ellipses (dots) before the argument name.

Let's see a simple example of PHP variable length argument function.

<?php  
function add(...$numbers) {  
    $sum = 0;  
    foreach ($numbers as $n) {  
        $sum += $n;  
    }  
    return  "Sum of number is ".$sum;  
}  
  
echo add(1, 2, 3, 4);  
?>

Output :
-----------------------
Sum of number is 10



No comments:

Post a Comment