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 array map
Feb 14, 2023 programming

PHP array map

Today we are going to learn about array map function in PHP. It lets you apply a function to the elements of the given arrays and returns an array with changed elements. ...

4 Min Read Read More
PHP curl
Mar 22, 2023 programming

PHP curl

Today we are going to talk about curl in PHP. Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP? curl is the answer. ...

6 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
3 Ways to add Conditional and Loop within HTML
Mar 21, 2023 programming

3 Ways to add Conditional and Loop within HTML

Today we are going to talk about different ways we can add PHP conditionals and loops within HTML . ...

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 ...