PHP Variable Length Argument Function PHP

PHP Variable Length Argument Function  

PHP Variable Length Argument Function

PHP Variable Length Argument Function

PHP supports variable length argument function. It means you 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.

The 3 dot concept is implemented for variable length argument since PHP 5.6.

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

  1. <?php  
  2. function add(...$numbers) {  
  3.     $sum = 0;  
  4.     foreach ($numbers as $n) {  
  5.         $sum += $n;  
  6.     }  
  7.     return $sum;  
  8. }  
  9.   
  10. echo add(1, 2, 3, 4);  
  11. ?>  

Output:

10

 

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries