Connecting Tech Pros Worldwide Help | Site Map

Passing an array as a parameter list

  #1  
Old November 21st, 2008, 10:35 PM
August Karlstrom
Guest
 
Posts: n/a
Hi,

In Perl you can pass the elements of an array to a function as actual
parameters, for example

my @a = (1, 2, 3);

f(@a);

which has the same effect as f(1, 2, 3). Does anyone know if there is a
construct in PHP to convert an array into a parameter list to achieve
the same thing?


August
  #2  
Old November 22nd, 2008, 02:05 AM
trookat
Guest
 
Posts: n/a

re: Passing an array as a parameter list


August Karlstrom wrote:
Quote:
Hi,
>
In Perl you can pass the elements of an array to a function as actual
parameters, for example
>
my @a = (1, 2, 3);
>
f(@a);
>
which has the same effect as f(1, 2, 3). Does anyone know if there is a
construct in PHP to convert an array into a parameter list to achieve
the same thing?
>
>
August
variables passing can only be standard way

$bar=foo($1,$2,$3)

however when declaring the function you have the options of ;

function foo($first,$second,$third)
{

}

or

function foo()
{
$first=func_get_arg(0);
$second=func_get_arg(1);
$third=func_get_arg(2);

}

or

function foo()
{
$args=func_get_args();
$first=$args[0]; // array first element is 0
$second=$args[1];
$third=$args[2];

}

all this however does not prevent you passing array for you function to
work on ;

$bar=foo(array('dude','where is my','car'));

function foo($args)
{
$first=$args[0]; // array first element is 0
$second=$args[1];
$third=$args[2];
}

which what your code translated to php would do , php will pass the
array not the individual elements as separate variables

hope that helps ( sorry for the long winded explanation its better then
just saying no)
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference to an Array of some dimension arnuld answers 7 August 14th, 2007 05:05 PM
pass an array as parameter to a function Josué Maldonado answers 1 November 23rd, 2005 02:05 AM
How can I pass a multidimensional array as a ref parameter in func vmsgman answers 21 November 17th, 2005 06:34 AM
Passing an array of chars to a function jr answers 58 July 22nd, 2005 06:36 AM