Viewed   85 times

Possible Duplicate:
PHP get all arguments as array?


In java I can do this (pseudocode):

public hello( String..args ){
    value1 = args[0] 
    value2 = args[1] 
    valueN = arg[n];

and then:

hello('first', 'second', 'no', 'matter', 'the', 'size');

Is something like this in php?


I now that I can pass an array like hello(array(bla, bla)), but may could exists the way mencioned above, right?



See func_get_args:

function foo()
    $numArgs = func_num_args();

    echo 'Number of arguments:' . $numArgs . "n";

    if ($numArgs >= 2) {
        echo 'Second argument is: ' . func_get_arg(1) . "n";

    $args = func_get_args();
    foreach ($args as $index => $arg) {
        echo 'Argument' . $index . ' is ' . $arg . "n";


foo(1, 2, 3);


When you call foo(17, 20, 31) func_get_args() don't know that the first argument represents the $first variable for example. When you know what each numeric index represents you can do this (or similar):

function bar()
    list($first, $second, $third) = func_get_args();

    return $first + $second + $third;

echo bar(10, 21, 37); // Output: 68

If I want a specific variable, I can ommit the others one:

function bar()
    list($first, , $third) = func_get_args();

    return $first + $third;

echo bar(10, 21, 37); // Output: 47
Wednesday, November 9, 2022

No, it's not possible to do directly. You might try this "clever" trick instead:

public function addPages(array $pages)
    foreach($pages as $page) addPage($page);

public function addPage(My_Page $page)

But I 'm not sure if it's worth all the trouble. It would be a good approach if addPage is useful on its own.

Thursday, October 27, 2022

If you wrote that function, you can make the 6th parameter optional:

function sendemail($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc = null) {
    if ($cc !== null) {
        // add cc headers, e.g.
        // $headers['Cc'] = $cc;

You will then have the option to omit this parameter:

sendemail("[email protected]", "[email protected]", "subject", "body", "[email protected]");
sendemail("[email protected]", "[email protected]", "subject", "body", "[email protected]", "[email protected]");
Friday, September 30, 2022

My suggestion is use array instead of using number of argument, For example your function call should be like this.

$params[6] = 'value';

For identify that sixth parameter has set

function MyFunction($params){
 If ( isset($params[6]) ) // parameter six has value


I hope that it will be a alternate way

Friday, October 7, 2022

func_get_args will do what you want:

function infinite_parameters() {
    foreach (func_get_args() as $param) {
        echo "Param is $param" . PHP_EOL;

You can also use func_get_arg to get a specific parameter (it's zero-indexed):

function infinite_parameters() {
    echo func_get_arg(2);

But be careful to check that you have that parameter:

function infinite_parameters() {
    if (func_num_args() < 3) {
        throw new BadFunctionCallException("Not enough parameters!");

You can even mix together func_*_arg and regular parameters:

function foo($param1, $param2) {
    echo $param1; // Works as normal
    echo func_get_arg(0); // Gets $param1
    if (func_num_args() >= 3) {
        echo func_get_arg(2);

But before using it, think about whether you really want to have indefinite parameters. Would an array not suffice?

Thursday, November 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :