2

I have defined a php function:

function my_function($text = '', $relative = false, $icon = true) {
//do something
}

It works as expected with default values. But when I want to change the value of one variable it doesn't work:

my_function($icon = false); // this doesn't change anything

I have to call it with all variables to make changes take effect:

my_function($text = '', $relative = false, $icon = false); // this changes the output

I am working in php 5.4.1 and Wordpress. What I do wrong? Thanks.

2
  • 1
    don't use the formal parameter names inside the parameter parts when "using" that function. so e.g. its "my_function(false)" in the first example. Commented May 2, 2012 at 20:48
  • That was proposed as a php feature a long time ago. It never made it in though. I liked the idea, although I cant say I'd use it all that often. Commented May 2, 2012 at 23:34

4 Answers 4

3

You must provide values for any default arguments to the left (in function signature) of the argument you want to change.

So given the function:

function my_function($text = '', $relative = false, $icon = true) {
//do something
}

Here are some examples:

// $text = "foo", $relative = false, $icon = true
my_function("foo"); 

// $text = "", $relative = true, $icon = true
my_function("", true) 

// $text = "", $relative = false, $icon = false
my_function("", false, false) 
Sign up to request clarification or add additional context in comments.

Comments

2
my_function($icon = false);

You can't do that in PHP. Well, you can, but it doesn't do what you think it does. What this does is:

  1. Sets $icon to false
  2. Passes false as the 1st parameter to my_function

If you want to change the third parameter, you need to also pass the first two.

my_function('', false, true);

Note: I think it's python that lets you call functions like that, to set only the Xth parameter.

4 Comments

I frequently use this syntax in PHP, but yes, it doesn't let one skip the previous parameters. I tend to use the old-style 'private' naming convention to indicate it shouldn't be reused: my_function($_icon = false);.
What do you mean by "old-style 'local' naming convention"? How does that indicate "it shouldn't be reused"?
It won't stop its reuse, of course. But its prefix is enough for me to know I won't use it elsewhere as a proper variable in the current scope.
Old style 'local' - my mistake, I meant 'private' (fixed). Prior to PHP getting a specific mechanism for class attribute visibility, the convention was (iirc) to use an underscore prefix for private variables.
1

Nothing wrong, that's how PHP works. And when you're calling a function, you shouldn't put variables in the parameter list as you might get undesired results as the variables will be assigned those values. So, this is fine:

my_function('', false, false);

or:

my_function($text, $relative, $icon);

or anything in between, no need to assign values.

Comments

1

You may use below code:

<?php

function my_function($arg = array("text"=>'Default',"relative"=>false,"icon"=>false))
{
  extract($arg);
      $text = isset($text)?$text:"";
      $relative = isset($relative)? $relative : false;
      $icon = isset($icon)? $icon : false;

      //do something

}

my_function(array("icon"=>true));

?>

WARNING: if you use this way. you should initialise the variable or check the if it is exists.

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.