1

Before completing this code, I just tested it by mistake and realized that it will not stop:

$var = "any"; 
for ($i=1; $i < 2; $i++){ 
    $var.$i = "any"; 
}

Why does this produce an infinite loop? And why doesn't PHP produce an error?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

4 Answers4

5

I did a simple test :

echo $i;
 $var.$i = "any";
var_dump($var);

Result :

1string(3) "any"
anzstring(3) "any"

So $i get transformed to "anz" and doesn't pass the validation to get out of the loop.

$var.$i = "any"; is not really correct, i don't know what you are trying to do, but if you want to fill and array you should do something more like :

$var = array();
for ($i=1; $i < 2; $i++){ 
 $var[] = "any";
}

If you want to change your string letter by letter :

$var = "any";
    for ($i=1; $i < 2; $i++){ 
     $var[$i] = "a"; // asign a new letter to the string at the $i position
    }
Morsok
  • 126
  • 1
  • 3
  • thanks morsok, I knew these, I just wonder why PHP not produce any error, if this `$var.$i` not valid then why PHP executes? –  Jul 25 '13 at 11:11
  • Strictly speaking it's valid, the behavior is simply not what you expect. Internally i can't tell you exactly what it does, but it seems to change $i to the string "any" with the last letter being added the value of $i making 'y' go 'z', and this is always < 2 hense it's goes to an infinite loop. – Morsok Jul 25 '13 at 11:14
  • @Skippy But I don't see any `z` here, ok, can you please explain what is `z`? –  Jul 25 '13 at 13:04
  • 1
    ok any way, thanks for your attention, I hope he decide if its z or y –  Jul 25 '13 at 13:08
  • 1
    It's a 'z', it didn't add space to the debug sorry about that, you should read the debug as : echo $i = "anz" var_dump($var) = .... – Morsok Jul 25 '13 at 13:19
2

When you do the following $var.$i = 'any' you set the $i variable and the $var variable. So the the loop never stop running because var_dump($i < 1) returns true.

$var = 'var';
$i = 1;
$var.$i = 'var';

var_dump($i); 

Returns string(3) "var".

This loop will never stop because $i is always reset to 'var', which is smaller than 1.

Benz
  • 2,317
  • 12
  • 19
1

This is incorrect $var.$i = "any"; because this expression is equivalent to:

 $var.($i = "any");

Which assigns $i to new value, therefore the condition of which the while loops checks will always be true.

Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
  • I knew that, what is incorrect? why makes the loop go for Infinity! –  Jul 25 '13 at 11:03
0

PHP5.4+.

You will get 'anz' result, after $i++, when $i == 'any'. $i == 'any', after assignment and that is, actually, what it should get. Trick is in "$i='any'" part of line. Even when "=" has lower precedence then ".", why do you think that it shouldn't put 'any' inside $i?

Try this instead:

$var = "any"; 
for ($i=1; $i < 2; $i++){ 
 $i.$var = "anything";
}

And your loop will work. And $var will get "anything" value. This doesn't look like a bug. Just unexpected behaviour for somebody.

QArea
  • 4,955
  • 1
  • 12
  • 22