RE: [PHP] expand array into function arguments?

From: Date: Wed, 11 May 2005 20:48:39 +0000
Subject: RE: [PHP] expand array into function arguments?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-214976@lists.php.net to get a copy of this message
> -----Original Message----- > From: Christopher J. Bottaro [mailto:cjbottaro@alumni.cs.utexas.edu] > Sent: Thursday, 12 May 2005 5:13 AM > To: php-general@lists.php.net > Subject: [PHP] expand array into function arguments? > > You can do this in Python: > > <code> > def myFunc(arg1, arg2, arg): > #do something > > myList = [1, "arg", 5] > myFunc(*myList) # calls myFunc(1, "arg", 2) > <code> > > Can that be done in PHP, and if so, how? Don't know if there's a better solution, but at the very least you could put the values in an array, and pass the array to the function. $arr = array(); $arr[] = "First"; $arr[] = "Second"; $arr[] = "Third"; $val = my_function($arr); function my_function($arr_in){ // do something with $arr_in[0]; // do something with $arr_in[1]; // do something with $arr_in[2]; } Note: if you wanted the function to be able to change the actual values in the array, you would need to pass it by reference rather than by value, eg: Function my_function(&$arr_in){ etc... The ampersand before the variable name in the function declaration indicates the variable is being passed by reference. Just a thought. Much warmth, Murray

« previous php.general (#214976) next »