Open In App

Variable-length argument list in PHP

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.



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).



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.

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.


Article Tags :