Reply to Variable scope issue

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация