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

Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
PHP catch exceptions
Feb 14, 2023 programming

PHP catch exceptions

Today we are going to talk about catching exceptions in PHP. Most of the time in your script you might face some exceptions. So today we are going to learn how to catch those exceptions. ...

7 Min Read Read More
PHP Dependency Management
Mar 22, 2023 programming

PHP Dependency Management

Today we are going to talk about dependency management in PHP. Your code might depends on some packages. You need a way to manage all the dependencies in PHP. ...

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