Re: Does passing an uninitialized array variable initialize it? (PHP 5)



Jerry Stuckle wrote:
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.


I take this back - the result of the operation is unpredictable. That's because:

$rank[$i] = trim($rank[$i++]);

$rank[$i++]

is evaluated as $rank[$i]. However, when

$rank[$i]

is evaluated, does it use the old or the new version of $i? Is it the old version or the new version? There's no indication in PHP, and in both C and C++ it is documented that the result is unpredictable.

So the effective action might be:

while (isset($rank[$i])) {
$tmp = trim($rank[$i]);
$i = $i + 1;
$rank[$i] = $tmp;
}

The bottom line: don't change a value and use it in the same statement. Results are unpredictable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: Novice needs help :)
    ... Jerry Stuckle skrev: ... There is no bug! ... a PHP function. ...
    (comp.lang.php)
  • Re: [PHP] Re: Function variables in classes
    ... I'm guessing that it's being run on a version php5, in which case the fatal error ... seems correct - you have a non-static method defined and then you are trying to ... In case this is truely a bug in PHP, ...
    (php.general)
  • Re: PHP5 Newbie
    ... Jerry Stuckle wrote: ... it to work with php5 and apache2. ... Warning: Unknown: ... Warning: Unknown: Failed to write session data. ...
    (comp.lang.php)
  • Re: [PHP] References challenge with PHP4
    ... don't be calling PHP5 "ALL THAT" when it can't do it right ... I have never run into this bug before ... ... named "references with global and static variables" ...
    (php.general)
  • Re: Does passing an uninitialized array variable initialize it? (PHP 5)
    ... For some reason PHP5 appears to be initializing $rankeach ... issettest on that element succeeds so the loop continues ... 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. ...
    (comp.lang.php)