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

Courses