your image

PHP Variable Length Argument Function - javatpoint

javapoint
Related Topic
:- PHP

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:

 

Features of Java - Javatpoint

10

Comments