You are here: Re: Does passing an uninitialized array variable initialize it? (PHP 5) « PHP Programming Language « IT news, forums, messages
Re: Does passing an uninitialized array variable initialize it? (PHP 5)

Posted by Paul Lautman on 07/30/07 21:20

google2006@sensation.net.au wrote:
> The following code never sees the end of the array, and generates an
> out of memory error under PHP5 (both CLI and Apache module) :
>
> while (isset($rank[$i])) {
> $rank[$i] = trim($rank[$i++]);
> }
>
> Moving the post increment to a separate line fixes this issue:
>
> while (isset($rank[$i])) {
> $rank[$i] = trim($rank[$i]);
> $i++;
> }
>
> For some reason PHP5 appears to be initializing $rank[($i+1)] each
> time, perhaps because it's passing the value to trim(). The subsequent
> isset() test on that element succeeds so the loop continues
> indefinitely as each new element is initialized.
>
> It works as expected under PHP4, stopping once it hits the end of the
> array. I can't find any documentation which hints at any change to the
> handling of uninitialized variables which caused this code to break.
>
> Thanks in advance for any help...

Regardless of anything else, in the expression trim($rank[$i++]), you are
referencing an uninitialised variable and that is bad programming practice
and should throw an error if error checking is set high enough.

 

Navigation:

[Reply to this 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

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