Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing an array as a parameter list

August Karlstrom
Guest
 
Posts: n/a
#1: Nov 21 '08
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

trookat
Guest
 
Posts: n/a
#2: Nov 22 '08

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