Marty
Marty

Reputation: 39456

What is the syntax for multiple arguments in PHP?

A simple question:

What is the syntax for creating a function with unlimited* arguments in PHP?

Example (ActionScript 3):

function multiTrace(...arguments):void
{
    var i:String;
    for each(i in arguments)
        trace(i);
}

The goal is to have a function that I can call and list any given amount of stylesheets within, eg:

$avian->insertStyles("overall.css", "home.css");

*Subject to obvious limitations (RAM, etc).

Upvotes: 2

Views: 118

Answers (3)

Rene Terstegen
Rene Terstegen

Reputation: 8046

See

http://nl.php.net/manual/en/function.func-get-args.php

function testfunction() {
    $arguments = func_get_args();

    var_dump($arguments);
}

testfunction("argument1", "argument2");

Result:

array
  0 => string 'argument1' (length=9)
  1 => string 'argument2' (length=9)

Upvotes: 3

Peter Kruithof
Peter Kruithof

Reputation: 10764

function multiTrace() 
{
    $args = func_get_args();
    while ($arg = array_shift($args)) {
        echo $arg; 
    }
}

see: http://php.net/manual/en/function.func-get-args.php

Upvotes: 7

Dan
Dan

Reputation: 16236

I think you are after the func_get_args function:
http://php.net/manual/en/function.func-get-args.php

Upvotes: 1

Related Questions