Forum Moderators: coopster

Message Too Old, No Replies

Variable number of arguments in function

One variable holds a variable number of arguments of a function

         

GavinTBS

6:44 pm on Mar 15, 2009 (gmt 0)

10+ Year Member



Hi,

I have a variable which holds a variable number of arguments that I need to pass to a function.

Any ideas how I can do this?

eg...

$other = "hello,gavin,test";
function($other);

Pretty much does function("hello,gavin,test")
I need it to do:

function("hello","gavin","test");

Baring in mind that the number of arguments in the $other variable is likely to change, and isn't a set number.

Any ideas?

Many thanks!

[edited by: GavinTBS at 6:44 pm (utc) on Mar. 15, 2009]

IanKelley

7:05 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey,

Something like this should work:

$other = "hello,gavin,test"; 
myfunction($other);
function myfunction($other) {
$input = explode(',',$other);
}

$input[0] = 'hello' etc...

[edited by: IanKelley at 7:06 pm (utc) on Mar. 15, 2009]

penders

11:59 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



function("hello","gavin","test");

I think IanKelley has probably answered your query in this instance, but it might be worth bearing in mind that in PHP you can have Variable-length argument lists [uk2.php.net] as you are suggesting here.