2

I know how to check with func_num_args for the number of arguments for a function, but how can I get these arguments inside the php function:

<?php
function test() {
        echo func_num_args(); //returns 2
        echo $argv[1]; //this doesn't work
}

test("aa","bb");

?>
0

2 Answers 2

2

You can call func_get_args() to obtain the array of arguments passed to the function.

For your example, simply add

$args = func_get_args();

And it should work as intended.

There also is func_get_arg, which returns a single argument:

echo func_get_arg(1); // prints second argument
Sign up to request clarification or add additional context in comments.

Comments

2

For PHP 5.6+ you can use: ... like this:

function xy(...$args) {

    foreach($args as $arg)
        echo $arg . "<br />";

}

For more information about this see the manual: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.