A variadic parameter allows a function to accept an arbitrary, indefinite number of arguments. Implemented using theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
... (ellipsis) token, it instructs the PHP engine to collect all remaining arguments passed at the call site into a single array bound to the parameter name. If positional arguments are passed, the resulting array is numerically indexed. As of PHP 8.0, if named arguments are passed, they are collected into an associative array where the keys correspond to the argument names.
Syntax
The... operator is prefixed directly to the parameter variable within the function signature.
Technical Constraints and Rules
- Positional Requirement: The variadic parameter must be the absolute last parameter declared in the function signature. Declaring any parameter after a variadic parameter results in a fatal compile-time error.
- Singularity: A function signature can contain a maximum of one variadic parameter.
- Default Values: Variadic parameters cannot have default values assigned in the signature (e.g.,
...$args = []is invalid syntax). If no arguments are passed for the variadic parameter, it initializes as an empty array[].
Named Arguments Integration (PHP 8.0+)
Variadic parameters fully support named arguments. When unknown or dynamically named arguments are passed to a function, the variadic parameter captures them as key-value pairs. The resulting array will contain a mix of integer keys for positional arguments and string keys for named arguments.Type Declarations
Variadic parameters fully support type hinting. When a type is specified, the constraint applies to the individual elements collected, not to the resulting array itself. The type declaration precedes the... operator.
Pass-by-Reference
Variadic parameters can be passed by reference by prefixing the... operator with the & (reference) operator. This binds the array elements directly to the memory addresses of the variables passed at the call site, allowing the function to mutate the original variables. This applies to both positional and named arguments.
Relationship to Argument Unpacking
The... token serves a dual purpose depending on its context. While using it in a function signature gathers arguments into an array (variadic parameter), using it in a function call performs the inverse operation (argument unpacking), expanding an array or Traversable object into discrete arguments. If the unpacked array is associative, PHP 8.0+ treats the string keys as named arguments.
Master PHP with Deep Grasping Methodology!Learn More





