Variable-length argument list in PHP Last Updated : 10 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a set of arguments whose length is unknown at present, we will see how a function can work with these unknown numbers of arguments whose quantity will vary depending on the requirement. We will take up each word one by one to deeply understand the topic we are dealing with. Variable: It is the number of arguments keep changing. Length: It refers to the number of arguments. Argument: It refers input passed to a function. Now, the point of interest lies at the word list, all the arguments passed at its call will go the function as an array. The values will be retrieved like they are being from an array. Accessing Variable Arguments Method: In this, the function is made to accept variable arguments and work accordingly. The variable which has to have multiple numbers of arguments is declared with "..."(triple dots). Example: php <?php function sum(...$numbers) { $res = 0; foreach($numbers as $n) { $res+=$n; } return $res; } echo(sum(1,2,3,4)."\n"); echo(sum(5,6,1)); ?> Output: 10 12 Providing Variable Arguments Method: You can also use "..."(triple dots) when calling functions to unpack an array or Traversable variable or literal into the argument list. Example: php <?php function add($a,$b) { return $a + $b ; } echo add(...[1, 2])."\n"; $a = [1, 2]; echo add(...$a); ?> Output: 3 3 Type hinted Variable Arguments Method: It is also possible to add a type of hint before the ... token. If this is present, then all arguments captured by ... must be objects of the hinted class. Example: php <?php function total_intervals($unit, DateInterval ...$intervals) { $time = 0; foreach ($intervals as $interval) { $time += $interval->$unit; } return $time; } $a = new DateInterval('P1D'); $b = new DateInterval('P2D'); echo total_intervals('d', $a, $b).' days'; ?> Output: 3 days Comment More infoAdvertise with us Next Article Variables and Datatypes in PHP V vanshikagoyal43 Follow Improve Article Tags : Web Technologies PHP PHP-Misc Similar Reads How to limit string length in PHP ? A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Table of ContentUsing for loopUsing mb_strimwidth() function Using substr() method Using Regular Expressions with preg_m 6 min read Variables and Datatypes in PHP A variable in PHP is a container used to store data. The value of a variable can change during program execution. PHP variables start with a dollar sign ($), followed by the variable name.$name = "GFG";$age = 30;Declaring Variables in PHPTo declare a variable, you simply use the dollar sign followed 4 min read PHP ob_get_length() Function The ob_get_length() function is an inbuilt function in PHP that is used to get the length of the current output buffer. The output buffer length is the number of bytes in the buffer. Syntax: ob_get_length(): int|falseParameters: This function does not accept any parameter. Return Values: The ob_get_ 2 min read PHP list() Function The list() function is an inbuilt function in PHP which is used to assign array values to multiple variables at a time. This function will only work on numerical arrays. When the array is assigned to multiple values, then the first element in the array is assigned to the first variable, second to th 2 min read PHP list() Function The list() function is an inbuilt function in PHP which is used to assign array values to multiple variables at a time. This function will only work on numerical arrays. When the array is assigned to multiple values, then the first element in the array is assigned to the first variable, second to th 2 min read PHP SplFixedArray offsetExists() Function The SplFixedArray::offsetExists() function is an inbuilt function in PHP which is used to check provided index exist or not in an array. Syntax: bool SplFixedArray::offsetExists( $index ) Parameters: This function accepts single parameter $index which specifies the requested index. Return Value: Thi 1 min read Like