Hey all,

I am sort of new to PHP, but have been programming in Perl for a while. In most cases when passing args in perl, we used to do it in this style:


sub myfunction {
     my $self = shift;
     my $args = shift;

 my $value1 = $args->{value1};
 my $value2 = $args->{value2};

 print "$value1, $value2";
}

$val1 = 'blah';
$val2 = '2';
$myvalue = $self->myfunction({value1=>$val1, value2=>$val2});

output:  blah, 2

I am not so concerned about the "shifts" and $self, because php has the $this variable and such. What I am mainly asking is it possible to pass arguments like this in php?

If that isn't possible, can you decide not to pass arguments to a function?

I.E. in PHP

function myFunction($value1, $value2, $value3) {
$val1 = $value1 if $value1;
$val2 = $value2 if $value2;
$val3 = $value3 if $value3;
}

myFunction('blah',,);

I hope this question makes sense.

Thanks in advance!

    What I am mainly asking is it possible to pass arguments like this in php?

    Yes, e.g.,

    function myfunction($value1, $value2) {
        echo "$value1, $value2";
    }
    
    $val1 = 'blah';
    $val2 = '2';
    $myvalue = myfunction($val1, $val2);

    If that isn't possible, can you decide not to pass arguments to a function?

    Yes (by not having an empty parameter list for the function), but I think you mean: can you decide to leave out arguments when calling a function? My answer would then be: it depends.

    If function has default arguments, you may leave out those parameters that provide default arguments, though you cannot include an argument after leaving out an argument that comes before it in the argument list. Or, the function might use variable arguments. However, PHP does not have named arguments at the moment, so the arguments must be specified positionally.

      Ah that kinda sucks....thanks for the info though.

      I guess you could just call a function, passing '0' for the args you don't want to use or don't know.

        I suppose you could pass an associative array of arguments:

        <?php
        class Test
        {
           function example($args=NULL)
           {
              if($args !== NULL && !is_array($args))
              {
                 user_error(__METHOD__ . ': arg is not an array');
                 return(FALSE);
              }
              $arg1 = (isset($args['arg1'])) ? $args['arg1'] : 'default value';
              $arg2 = (isset($args['arg2'])) ? $args['arg2'] : 'default value';
              return("arg1 = $arg1 and arg2 = $arg2");
           }
        
        }
        $test = new Test();
        echo $test->example(array('arg2' => 'value 2', 'arg3' => 'value 3'));
        

          hah, thats awesome NogDog. Although it seems kinda hackish compared to the Perl style, it definetely gets across what I would like to do.

          Is there any reason that this would be considered bad practice or inefficient in a site that could potentially get LOTS of hits per hour (considering the array wouldn't be more than 5 args probably, so don't have to worry about exceedingly large arrays) ?

            Well, since I can't off-hand think of another way to do it, I'd have to say it's the best-performing method I know of to avoid PHP's dependence on positional function parameters. 🙂 I'm sure it does not perform as well as using a normal function parameter list, but whether we're talking a difference of nanoseconds or milliseconds I couldn't say.

              Write a Reply...