Passing an array as a parameter list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • August Karlstrom

    Passing an array as a parameter list

    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

    #2
    Re: Passing an array as a parameter list

    August Karlstrom wrote:
    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,$sec ond,$third)
    {

    }

    or

    function foo()
    {
    $first=func_get _arg(0);
    $second=func_ge t_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)

    Comment

    Working...