1

This line of code worked perfectly before I upgraded, now it doesn't:

123: $thisRow[] = displayR(${"sR{$i}S1"}, ${"nR{$i}S1P"});

I get this error:

Undefined variable: sR1S1 in script.php on line 123

A workaround is to manually assign them first

$s = ${"sR{$i}S1"};
$n = ${"nR{$i}S1P"};
$thisRow[] = displayR($s, $n);

Is there anyway to get it working in a single line as before? Does anyone know why it no longer works?

I should add that I've tried using

var_dump(${"sR{$i}S1"});
on the previous line and it IS defined, and has the value that I expect it to.

I can't find any mention of this behaviour on google or SO, I wonder if it's a bug rather than intended.

Codemonkey
  • 4,455
  • 5
  • 44
  • 76
  • 1
    Clearly tightening up on sloppy coding practises is a bug: if a variable isn't defined when you pass it to a function, what is that function supposed to do? – Mark Baker Mar 02 '15 at 12:54
  • [php notice undefined variable and notice undefined index](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Mark Baker Mar 02 '15 at 12:56
  • The variables ARE defined @Mark. I can var_dump them on the previous line and they're there, as inteded. var_dump(${"sR{$i}S1"}) outputs a value, as expected. – Codemonkey Mar 02 '15 at 12:57
  • Well if you consider that it's a bug, [flag it as a bug](https://bugs.php.net/) to the PHP core devs – Mark Baker Mar 02 '15 at 12:58
  • In good time I will, but I thought I should tap into the near-infinite SO wisdom before bugging (hah) the limited resources of the PHP core devs! – Codemonkey Mar 02 '15 at 12:59
  • However, it doesn't give any error on my 5.6.6.... or on [3v4l.org demo](http://3v4l.org/QfK0t) – Mark Baker Mar 02 '15 at 13:02
  • The behaviour seems to be consistent across versions, see http://3v4l.org/sr9bn – SirDarius Mar 02 '15 at 13:05
  • 1
    Let me put it this way: using variables such as this instead of arrays reeks of terrible practice. We do not have any conclusive proof that this program *should* work, because we have no complete test case. The test cases which were reproduced above all work. Given all this, I find it more likely that your program has some bug, instead of PHP. You'll have to provide a complete reproducible example for us to be able to say otherwise. – deceze Mar 02 '15 at 13:09
  • That's a great resource that is new to me, and it's good to know that it SHOULD work. I'll try to suss out what's gone wrong and report back. All I know so far is that this 100% changed from working to broken when I upgraded PHP this morning... – Codemonkey Mar 02 '15 at 13:10
  • Turns out it's an intermittent problem. If I var_dump($sR1S1) and hit F5 it sometimes fixes everything. And sometimes things remain fixed even when I remove the var_dump. On my server this is. Weirdness. Definitely feels like a bug to me now. Can't replicate on 3v4l. – Codemonkey Mar 02 '15 at 14:08
  • ...which lead me to opcache. And it seems that disabling opcache fixes this "problem" completely. For whatever reason. – Codemonkey Mar 02 '15 at 14:22
  • Bug report submitted, bug apparently fixed. – Codemonkey Mar 04 '15 at 12:53

1 Answers1

0

I believe this is to be an opcache bug.

Disabling opcache on the file in question resumed the expected behaviour.

I submitted a (fairly useless) bug report at:

https://bugs.php.net/bug.php?id=69159

Which has apparently been fixed and closed:

http://git.php.net/?p=php-src.git;a=commitdiff;h=a29b64fc1029b4121e3e9ff20901c35ad600b4da

Codemonkey
  • 4,455
  • 5
  • 44
  • 76