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 CSV Files
Mar 21, 2023 programming

PHP CSV Files

Today we are going to talk about working with a csv files in PHP. Comma Separated Values (csv) is a common way to store the data in a file and it's important to know how to work with them. ...

5 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 Password Hashing
Mar 22, 2023 programming

PHP Password Hashing

Today we are going to talk about hashing passwords in PHP. Storing and managing passwords is very important. And in PHP it’s very easy. ...

4 Min Read Read More
PHP Send SMTP Emails with PHPMailer
Mar 22, 2023 programming

PHP Send SMTP Emails with PHPMailer

Today we are going to send emails with the help of PHPMailer package. Sending emails is a very useful feature and in PHP it is very easy and powerful. ...

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