0

I came across this line:

list($diff, $current, $concurrent) = $diff;

Documentation states that this should result in undefined behaviour. What are the possible variants of this behaviour? Variable $diff is array, containing 3 elements with variable content.

This line is part of application that contains a bug and author of this line is unavailable. Though I am almost sure that it is not what I am looking for, it would be nice to be 100% sure. I am using PHP 5.6.25 as FPM/FastCGI. Thanks in advance.

6
  • Can you edit the application code? If so $array = $diff; list($diff, $current, $concurrent) = $array; will eliminate any undefined behavior. Commented Nov 24, 2016 at 14:05
  • It's undefined for a reason, it's likely to have a different effect depending on your version/operating system etc. You are overwriting $diff with $diff whilst it's been evaluated. Perhaps doing what @Steve suggested or changing the list($diff... variable will help. Commented Nov 24, 2016 at 14:07
  • Would be nice if I got 2 more comments on improving my question, instead of 2 downvotes. Commented Nov 24, 2016 at 15:23
  • @Steve Of course I will remove potentially undefined behaviour, but I would like to know the exact possibilities. Hope I am not asking for too much. Commented Nov 24, 2016 at 15:25
  • @R.Chappell Can you give a couple of actual examples on how can this or similar thing work differently? Commented Nov 24, 2016 at 15:27

1 Answer 1

1

As the documentation for list() also states:

In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-most parameter.

In other words: This line might work as intended in PHP 5, because the variable $diff that appears on both sides is the last variable to get assigned. However, in PHP 7 the $diff variable gets assigned first, so $diff has already changed by the time the assignments for $current and $concurrent are done.

In general I think the hint about undefined behaviour relates to the fact that you cannot rely on certain assignments to yield the expected results, if a variable appears on both sides of the = sign. A workaround for the issue could look like this:

list($temp, $current, $concurrent) = $diff;
$diff = $temp;
unset($temp);

This way you avoid the undefined behaviour.

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

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.