Free cookie consent management tool by TermsFeed Generator PHP Splat Operator | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Splat Operator

PHP Splat Operator

Last Updated on Feb 15, 2023

Introduction

Splat operator is when we add three dots before the name of the variable and it's way more useful than you think.

…$variableName

Use Cases

the splat operator can be used to 

  1. unpack parameters to functions  
  2. to combine variables into an array

Let’s see an example of both use cases.

function sayHello($name,$age){
   echo "hello my name is $name and I am $age years old";
}
$person = ['amir',28];
sayHello($person);

This will give me an error.

Now let’s see it with splat operator

function sayHello($name,$age){
   echo "hello my name is $name and I am $age years old";
}
$person = ['amir',28];
sayHello(...$person);
// hello my name is amir and I am 28 years old

It works!

If my array had more than 2 items it would still work because it would take the first to item of the array and use them in my function. But if it had less than 2 items I would get an error because both of the variables in my function don’t have any default value.

To check passing variable to functions read this post

Now let’s see an example of the second use case

I want to name some of my friends

function myFriends($personA,$personB){
   echo "$personA is my friend";
   echo "$personB is my friend";
}
myFriends('pratham','simon');

If I add a third friend my function won’t use the name, so I have to add that to the argument as well.

function myFriends($personA,$personB,$personC){
   echo "$personA is my friend";
   echo "$personB is my friend";
}
myFriends('pratham','simon','hassib');

It can get repetitive and boring so quickly.

But with the help of splat operators I can change all the arguments to an array

function myFriends(...$names){
   foreach($names as $name){
       echo "$name is my friend";
   }
}

Now even if I pass 100 arguments it will still work

function myFriends(...$names){
   foreach($names as $name){
       echo "$name is my friend";
   }
}

myFriends('pratham','simon','hassib','meet','vitto','francesco','christoph');
// pratham is my friend
// simon is my friend
// hassib is my friend
// meet is my friend
// vitto is my friend
// francesco is my friend
// christoph is my friend

Some of the functions in php use this splat operators for their arguments. For example you can pass as many argument as you want to var_dump and it will dump all of them.

var_dump(1,3.14,['php','twitter'],'amir');
// int(1)
// float(3.14)
// array(2) {
//  [0]=>
//  string(3) "php"
//  [1]=>
//  string(7) "twitter"
// }
// string(4) "amir"

Isn’t it great?

Conclusion

Now you know splat operator in PHP.

I recommend you to open a PHP files and try use the splat operator. try to practice both use cases.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • Splat Operator in PHP
  • Splat Operator Use Cases

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP function
Mar 21, 2023 programming

PHP function

Today we are going to learn about functions in PHP. Functions are very important because they can help you divide your application in small bites that can be handled with a function. ...

10 Min Read Read More
PHP Abstract Class
Mar 22, 2023 programming

PHP Abstract Class

Today we are going to talk about abstract classes in PHP. Abstract class is another important topic in object oriented programming and today we are going to learn everything about them. ...

8 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

6 Min Read Read More
How to Split an Excel File into Multiple Files Using PHP
May 07, 2024 programming

How to Split an Excel File into Multiple Files Using PHP

Sometimes you might have an excel files that you need to split. splitting the file into multiple files with PHP can be easily done. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...