Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to always pass all arguments to PHP function? [duplicate]

Possible Duplicate:
PHP - Calling functions with multiple variables

function test($var1=null, $var2=null, $var3=null){
    //smart stuff goes here
}

Do I have to every time call the function passing all variables?

test(null, $var2, null);

I'd like to pass only $var2 because all the other variables have default values... Is it even possible?

In JavaScript we can pass an object to the function, is there something similar in PHP?

like image 992
simPod Avatar asked Feb 03 '26 08:02

simPod


1 Answers

You only have to pass the arguments up to and including the last argument you do not wish to use the default value for. In your example, you could do this:

test(null, $var2);

The last argument can be omitted since the default value is satisfactory. But the first one must be included so PHP knows that you are setting the value for the second parameter.

Until PHP offers named parameters like Python does, this is how it has to work.

like image 173
John Conde Avatar answered Feb 05 '26 22:02

John Conde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!