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!
12 Comment(s)
Ahaa, its pleasant dialogue about this paragraph here at this blog, I have read all that, so now me also commenting here.
I am sure this piece of writing has touched all the internet users, its really really good article on building up new webpage.
I am sure this article has touched all the internet people, its really really nice piece of writing on building up new blog.
I think other website proprietors should take this web site as an model, very clean and excellent user friendly style and design, as well as the content. You're an expert in this topic!
I’ll immediately seize your rss feed as I can’t to find your e-mail subscription link or e-newsletter service. Do you’ve any? Kindly allow me recognise so that I may just subscribe. Thanks.
I will right away take hold of your rss as I can not to find your email subscription link or e-newsletter service. Do you've any? Kindly permit me realize in order that I could subscribe. Thanks.
Hi, I do believe this is an excellent site. I stumbledupon it ;) I will come back once again since i have bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.
Everyone loves what you guys tend to be up too. This type of clever work and coverage! Keep up the superb works guys I’ve incorporated you guys to my personal blogroll.
I wanted to thank you for this great read!! I certainly loved every little bit of it. I've got you book marked to look at new stuff you
Ahaa, its nice dialogue about this paragraph here at this weblog, I have read all that, so at this time me also commenting here.
You have made some good points there. I looked on the web for additional information about the issue and found most people will go along with your views on this web site.
I really like what you guys are usually up too. This kind of clever work and reporting! Keep up the very good works guys I've incorporated you guys to my blogroll.
Leave a Comment