Skip to content

Instantly share code, notes, and snippets.

@prashantdsala
Last active March 10, 2023 05:29
Show Gist options
  • Save prashantdsala/2d065a1091f0448654a32882fbf6860f to your computer and use it in GitHub Desktop.
Save prashantdsala/2d065a1091f0448654a32882fbf6860f to your computer and use it in GitHub Desktop.
Splat/Variadic operator (3 dots ...) in PHP
<?php
// The splat operator (also known as the variadic operator) in PHP is represented by three dots (...)
// It allows you to pass an arbitrary number of arguments to a function or method.
// Here's an example of using the splat operator in PHP:
function addNumbers(...$numbers) {
$sum = 0;
foreach ($numbers as $number) {
$sum += $number;
}
return $sum;
}
// You can call the function with any number of arguments
$result1 = addNumbers(1, 2, 3); // Result: 6
$result2 = addNumbers(4, 5, 6, 7, 8); // Result: 30
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment