7

I inherited a php codebase that contains some variable assignments in function calls:

<?php
function some_func($foo, $state) {
  ....
}

some_func("random stuff", $state = true);
...
some_func("other stuff", $state = false);
...
?>

I did some research and some tests, but I can't find out what the defined behaviour for this code is in PHP.

How is the value of the second argument to some_func() computed? The content of the 4state variable (true on first call, false on second)? Or is it the outcome of the assignment (i.e. assigning true/false to the variable $state was successful, so some_func received true?

What is the value of the $state variable in the global scope? The result of the assignment, i.e. true after the first call, false after the second?

1
  • This does not look right. Commented Nov 14, 2013 at 9:45

2 Answers 2

5

I too had to work with a codebase that had function calls similar to this. Luckily, I had access to developers that wrote the code. Here is what I learned.

Scenario 1:

Simply a way to document the code. You know the variable name that you are passing into the function.

Scenario 2:

Here is a link: http://www.php.net/manual/en/language.references.pass.php If you see, they do specifically call out your case:

foo($a = 5); // Expression, not variable

A 'dummy' pass-by-ref. Depending on your version of PHP, it may throw a warning. I was getting this: Strict Standards: Only variables should be passed by reference in ...

Now let me go into detail of what is happening in this situation.

The dangerous thing is that your example that you have provided wont display the "gotcha!" behavior. In a case like this, your $arg2 that you are echoing outside of the function will always be what the expression in the function call set it to be. Furthermore, the function that is being called will also be sent a "copy" of that value, and work with that. I say "copy" because even though the function is requiring a pass-by-ref, it is actually getting a copy, similar to what a normal function parameter would get.

If you modify the $arg2 that is inside of the function it WILL NOT modify the $arg2 that is outside of the function, as you would expect from a function that is pass-by-ref.

Sign up to request clarification or add additional context in comments.

2 Comments

The reference page even explains for the specific example: No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid. So this is actually undefined behaviour. Since the value isn't used outside the function, I'll just replace it with some_func("random stuff", true);. Thanks :)
You are right, it isn't outside of the function and thus would be safe to just set as is. Best of luck!
1

To assign a variable at function call time, you have to pass it as a reference (&$var):

function my_function($arg1, &$arg2) {
  if ($arg1 == true) {
    $arg2 = true;
  }
}
my_function(true, $arg2 = false);
echo $arg2;

outputs 1 (true)

my_function(false, $arg2 = false);
echo $arg2;

outputs 0 (false)

How is the value of the second argument to some_func() computed?

It's not "computed" but explicitly setup : $state = true / false and then passed as argument to some_func().

What is the value of the $state variable in the global scope?

$state does not exist in the global scope.

1 Comment

I know how to pass stuff around by reference, but that doesn't explain how the posted code functions. I want to replace it with something that works exactly the same, but is more understandable, but to do that I need to find out what it actually does.

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.