0

Is it possible to pass a variable to function without passing the variables that come before it in the function definition?

For example, if my function looks like this:

function doThis( $v1, $v2, $v3 )
{
    echo $v1.$v2.$v3;
}

Can I then call the function with only some of those variables, like doThis("v1",null,"v3") or some other way?

1
  • 2
    Questions like this can be answered very simply: Just try it! Commented Sep 29, 2011 at 21:44

6 Answers 6

2

you can use predefined arguments:

function doThis( $v1, $v2 = "hello", $v3 = 1 ) {}

call to this function only with the first argument, the 2nd and 3rd will be "hello",1 by defualt:

doThis(10);
Sign up to request clarification or add additional context in comments.

3 Comments

The situation is this: There is a function with some variables already set that I'm trying to add a variable to. None of the variables in the function definition have defaults set. I want to add a variable to the end, but I don't want to have to put in values for the other variables before it when I call the function. Does that make sense?
read the Q again. (I knew these answers are going to be upvoted. sigh)
you could add the additional variables to the end of the function, with a default valus just like my answer, that will not efect the previus calls to this function (as it has default values to the new parameters)
2

+1 on @Alon post

You can also use an array ..

$array['var1'] = null;
$array['var2'] = "foo";
$array['var3'] = "bar";


doThis($array);

function doThis($theArray){
 var_dump($theArray);
}

This way, you can use empty values and N amout of variables.

Comments

1

Yes, you can do doThis("v1",null,"v3"), if you prepare your function to handle that null argument.

For example your example will work just fine.

Or, if it makes sense you can change the order of the arguments so you can set defaults.

2 Comments

Huh, I must have some other problem in my code that is preventing it from working. Thanks though, glad you could understand the question better than the other posters. I'll accept it once SO lets me.
Just do if ($v2 !== NULL) (or ===) and handle the special case.
1

You can specify default values in the function definition like so:

function doThis($a, $b=null, $c=null) {
    echo $a . $b . $c;
}

doThis("hello", " world"); // yields: hello world

In this function, $a is a required parameter. The other two values are optional.

To appease the commenter below, you can of course still call this function like so:

doThis("hello", null, "world");

and of course, you do not have to provide defaults (but it is a good idea to do so)

1 Comment

Dear humor herold, perhaps you should read it again. "Is it possible to pass a variable to function without passing the variables that come before it in the function definition?
1

The answer is: rework your workflow.

I know you don't like that answer, but honestly, if you are passing more than 3 parameters or if sometimes you don't want to pass part of the beginning parameters, you probably want to rethink what you're doing. Function and method calls should be simple. You are probably doing too much in your function.

General tips:

  • Use classes if appropriate. This way you can avoid passing some information around because it's stored in the class and your methods have access to it.
  • Keep methods cohesive. That means your methods and functions should do one thing and do it well.
  • Put variables you want to pass only sometimes at the back. This allows you to simply not pass them if they have $var=null after it. You then check to see if $var===null before doing something with it.

Comments

0

Much cleaner way is passing an array of variables, as demonstrated by @jflaflamme.

Another way is allowing an arbitrary number of variables like this:

function foo()
{
    $arguments = func_get_args();

    if(sizeof($arguments))
    {
        echo implode('', $arguments);
    }
}

foo(1, 2, 3, $some_var);

You can also loop trough the array you obtain via func_get_args, check them for their type (if it's a null, array, object - you name it) and then handle them in a way you deem appropriate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.