Last active
March 10, 2023 05:29
-
-
Save prashantdsala/2d065a1091f0448654a32882fbf6860f to your computer and use it in GitHub Desktop.
Splat/Variadic operator (3 dots ...) in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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