( … ) Variable-length argument lists

In programming, there are many ways to solve a problem. But, everyone likes to easy and short solution.

The reason of telling that, PHP has a syntax token that will make my day. This is called Argument unpacking token or Variable-length argument lists token. This is typed with three dots ( … ). This is safe to use for 5.6 or latest version. For example,

function sum( ...$numbers ) {
    $acc = 0;
    foreach ( $numbers as $n ) {
        $acc += $n;
    }
    return $acc;
}

echo sum( 1, 2, 3, 4 );

return output 10 as 1+2+3+4=10.

Leave a Reply

Your email address will not be published. Required fields are marked *