Variable-length argument list in PHP Last Updated : 10 May, 2020 Comments Improve Suggest changes 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 Variable-length argument list 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 array_is_list() Function The array_is_list() function is an inbuilt function in PHP that is used to check whether a given array is a list or not. If the given array will be a list if the key of the array contains consecutive numbers from 0 to count($arr)-1. Syntax:Â bool array_is_list(array $array) Parameters: This function 1 min read PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi 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 How to Check Whether a Variable is Empty in PHP? Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, 5 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 PHP openssl_cipher_key_length() Function The openssl_cipher_key_length() function is an inbuilt function in PHP that is used to retrieve the key length required for a given cipher algorithm. Syntax: openssl_cipher_key_length(string $cipher_algo): int|false Parameters: This function accepts only one parameter which is described below. $ciph 1 min read PHP | SplFileInfo getSize( ) Function The SplFileInfo::getSize() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get file size in bytes. Syntax: int SplFileInfo::getSize( void ) Parameters: This function does not accept any parameter. Return values: This function returns the size of file in bytes. B 1 min read Like