Posted by rlee0001 on 06/01/06 17:31
This is seriously blowing my mind.
$a = array('a', 'b', 'c');
for ($i=0; $i<=count($a); $i++) {
echo 'In FOR: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
if ($a[$i] = 'a') {
echo 'In IF: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
}
}
Ok, I expected the above to output:
In For: a - b - c
In IF: a - b - c
In For: a - b - c
In For: a - b - c
But I get this:
In For: a - b - c
In IF: a - -
In For: a - b - c
In For: a - b - c
For some reason $a[1] & $a[2] can be seen outside the IF but inside the
IF always returns NULL except when the element in the IF is used as a
condition (like $a[0]). I've been coding in PHP for a while now and I
have never noticed IF blocks having their own scope. But I am using an
older version of PHP (4.3.3) on this server (not my choice) which I've
never used before. If IF statements do have their own scope GLOBAL
doesn't fix it because I tried that and got the same results.
I think I'm loosing my mind. Please help.
-Robert
Navigation:
[Reply to this message]
|