PHP - Spread Operator (...)
The spread operator (...) in PHP is used to unpack arrays and pass multiple arguments to functions. It simplifies array handling and function calls by spreading elements from an array into individual arguments.
The spread operator was introduced in PHP 5.6 for function arguments and later expanded in PHP 7.4 to allow array unpacking.
1. Using the Spread Operator in Function Arguments
The spread operator allows passing an array’s elements as individual arguments to a function.
Example:
Output:
Explanation:
-
The array
$numbersis spread into three separate arguments (2, 4, 6), which are passed to thesum()function. -
Without the spread operator, you would need to manually extract values from the array.
2. Using the Spread Operator in Variadic Functions
A variadic function can accept an arbitrary number of arguments using ... in the parameter list.
Example:
Output:
Explanation:
-
The
multiply()function accepts multiple arguments, which are packed into an array using...$nums. -
array_product()multiplies all the numbers.
3. Using the Spread Operator to Merge Arrays (PHP 7.4+)
Starting from PHP 7.4, the spread operator can be used to merge arrays.
Example:
Output:
Explanation:
-
Both
$arr1and$arr2are spread into a new array, merging them into one. -
This is more readable than using
array_merge($arr1, $arr2).
4. Combining Arrays with Additional Values
You can also mix arrays with individual values.
Example:
Output:
Explanation:
-
The spread operator allows inserting additional elements anywhere in the array.
Key Takeaways
✅ The spread operator (...) unpacks arrays into separate values.
✅ It can be used in function arguments, variadic functions, and array merging.
✅ From PHP 7.4+, it provides an easier way to merge arrays without using array_merge().
Conclusion
The spread operator in PHP makes working with arrays and functions more flexible and concise. Whether passing function arguments dynamically, merging arrays, or creating variadic functions, it simplifies code readability and efficiency.
Start using the spread operator today to write cleaner, more efficient PHP code!
14 Comment(s)
I will immediately seize your rss as I can not to find your email subscription link or newsletter service. Do you’ve any? Kindly allow me know in order that I may just subscribe. Thanks.
Way cool! Some very valid points! I appreciate you penning this write-up and also the rest of the site is also very good.
I do not even know the way I ended up here, but I believed this submit was once great. I do not understand who you're but definitely you're going to a well-known blogger when you are not already. Cheers!
Hi, I do believe this is an excellent blog. I stumbledupon it ;) I may return once again since i have book marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide other people.
There is definately a lot to find out about this issue. I really like all the points you’ve made.
Thanks for some other fantastic article. Where else may anybody get that type of information in such an ideal way of writing? I've a presentation subsequent week, and I am at the search for such information.
Greetings! I've been following your weblog for some time now and finally got the bravery to go ahead and give you a shout out from Dallas Tx! Just wanted to say keep up the good work!
Regards for this post, I am a big big fan of this web site would like to go on updated.
Hey there! I've been following your web site for a while now and finally got the courage to go ahead and give you a shout out from Kingwood Texas! Just wanted to mention keep up the fantastic job!
I am truly thankful to the holder of this site who has shared this enormous article at at this place.
Wow, this paagraph iis fastidious, myy sisterr iss analyzing such things, ths I am goijg tto convewy her.
I really like what you guys are up too. This sort of clever work and exposure! Keep up the very good works guys I've incorporated you guys to my blogroll.
These are truly impressive ideas in concerning blogging. You have touched some fastidious factors here. Any way keep up wrinting.
Greetings! Very helpful advice within this post! It is the little changes that make the greatest changes. Thanks a lot for sharing!
Leave a Comment