The splat operator is used as three dots before variable
(...) . It was introduced in PHP 5.6 but used rarely. In a simple definition, splat operator is used to unpack parameters to functions or to combine variables into an array.
It is also possible to use the splat operator to create functions with a variable number of parameters. By adding the operator to the function declaration we are essentially saying that the function can receive a variable number of inputs, so the function should be set up accordingly to process multiple items.
This above function will output 15. The parameters are sent to the function as array and we handled the array accordingly. We can also use type hint.
Another example of splat operator is that it can be used to split an array. As a result, we can sent the array to the function and it will be unpacked into different variables. Look at the following example:
We defined an array with 2 items. After we called the function implementing splat operator with array, it splits the array item separately and return the sum. So, the above code will echo
A simple example of splat operator can be found in
dd function in Laravel. It takes any number of arguments without having to pass them as an array.