Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arbitrary Number Of Argument

PHP allows any number of arguments to be passed to a function call. In the function definition, receiving those arguments means managing an arbitrary number of parameters.

This is achieved with the variadic operator, or with functions such as func_get_args().

They are also called variable argument lists.

When the arguments are dynamic, aka they are collected at execution time, the variadic operator or the function call_user_func_array() turns an array to arguments.

<?php

    function trumpet(...$args) {
        print "This function is called with ".count($args)." arguments\n";
        print "This function is called with ".func_get_count()." arguments\n";
        print "This function is called with ".count($func_get_args())." arguments\n";
    }

    $array = range(0, rand(5, 10)); // generates an array with 1 to 11 elements

    trumpet(...$array);
    call_user_func_array('trumpet', $array);

?>

Documentation

See Also