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

Your name:

Reply:


Posted by Rik on 07/30/07 21:27

On Mon, 30 Jul 2007 22:56:19 +0200, <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] =3D trim($rank[$i++]);
> }
>
> Moving the post increment to a separate line fixes this issue:
>
> while (isset($rank[$i])) {
> $rank[$i] =3D 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.


Hmmz, this test says it doesn't (PHP 5):
<?php
$arr =3D array();
$i =3D 0;
trim($arr[$i++]);
var_dump($arr);
?>
Result (without obvious notices):
array(0) {
}

If it works for you, it seems to me they fixed something in PHP5 that di=
d =

not work as _I_ would expect in PHP4: it seems $i on the left-hand side =
is =

evaluated _after_ the running of the right hand side in PHP5, and _befor=
e_ =

in PHP4, which this example shows: it overwrites the variables present:

<?php
echo phpversion();
$arr =3D array(1,2,3,4);
$i=3D0;
$save =3D 0;
while (isset($arr[$i])) {
$arr[$i] =3D trim($arr[$i++]);
if(++$save > 5) break;
}
var_dump($arr);
?>
Output:
5.2.2array(7) {
[0]=3D>
int(1)
[1]=3D>
string(1) "1"
[2]=3D>
string(1) "1"
[3]=3D>
string(1) "1"
[4]=3D>
string(1) "1"
[5]=3D>
string(1) "1"
[6]=3D>
string(1) "1"
}

I'm a bit struggling for words here, but see it as this: finding the =

reference to which to couple the output of your code happens after =

evaluating in PHP5, in PHP4 it first determines what the actual target i=
s.

I have no idea where to start finding when and how this behaviour change=
d, =

but I bet that handling references somewhat differently has something to=
=

do with it.
-- =

Rik Wasmus

[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

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