Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Mon, 30 Jul 2007 17:21:35 -0400
google2006@xxxxxxxxxxxxxxxx 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...
I did a quick look and don't see where this was behavior was explicitly changed, but it looks more like a bug to me.
$i++ returns the old value of $i, so $rank[$i + 1] should never be evaluated, IMHO.
I'd suggest putting it up as a bug on php.net and see what they say about it.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- Follow-Ups:
- Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- From: Jerry Stuckle
- Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- References:
- Does passing an uninitialized array variable initialize it? (PHP 5)
- From: google2006
- Does passing an uninitialized array variable initialize it? (PHP 5)
- Prev by Date: Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- Next by Date: Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- Previous by thread: Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- Next by thread: Re: Does passing an uninitialized array variable initialize it? (PHP 5)
- Index(es):
Relevant Pages
|