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

Variable Arguments

Variable arguments refer to a method call where the list of arguments depends on the call, rather than the signature of the method.

Variable arguments are achieved with the variadic operator, or with the func_get_args() function. The variadic operator covers most of the cases, and func_get_args() covers the remaining edge cases.

Variable arguments may be static or dynamic. It is static when the list of arguments varies from call to call, and is hard-coded. A dynamic argument list depends on the variadic ... operator, or the call_user_func_array() function.

<?php

    // No arguments in the signature.
    function foo() {
        // displays the list of arguments
        print_r(func_get_args());
    }
    
    // static arguments
    foo(1, 2);
    foo(4, 5, 6);
    
    // dynamic variable arguments
    $args = range(5, rand(9, 11)) ; 
    foo(...$args);

?>

Documentation

See Also