June 5, 2025

What is the three dots mean in PHP

In PHP, the three dots (...) are called the splat operator or variadic operator, and they serve two main purposes, depending on where they are used:

1. Function Parameter – Variadic Functions

When used in a function definition, ... allows you to accept a variable number of arguments:

function sum(...$numbers) {
    return array_sum($numbers);
}

echo sum(1, 2, 3, 4); // Outputs: 10
  • Here, $numbers becomes an array of all the arguments passed.
  • Very useful when you don’t know how many values will be passed in.

2. Function Call – Argument Unpacking

You can use ... to unpack an array into a list of arguments when calling a function:

function greet($greeting, $name) {
    echo "$greeting, $name!";
}

$args = ['Hello', 'Jigang'];
greet(...$args); // Outputs: Hello, Jigang!

3. Array Unpacking (PHP 7.4+)

You can also unpack arrays directly into another array:

$a = [1, 2];
$b = [3, 4];
$combined = [...$a, ...$b];

print_r($combined);
// Output: [1, 2, 3, 4]