|
Posted by comp.lang.php on 08/23/06 14:45
Andrew Poelstra wrote:
> "comp.lang.php" <phillip.s.powell@gmail.com> writes:
> > Well, this is what I found out:
> >
> > No matter what I put within the foreach loop, the loop ran infinitely,
> > and this is why:
> >
> > It constantly read $key as the very first element in $array, in short,
> > it never iterated in the first place!
> >
> > This only happens when I do this;
> >
> > function doStuff(&$array) {
> > if (is_array($array) && @sizeof($array) > 0) {
> > foreach ($array as $key => $val) print_r("key = $key<P>"); //
> > PRINTS "key = username" infinitely
> > }
> > }
> >
> >
> > What I suspect at this point that this is a PHP 4.3+ bug. I had
> > someone else in my DC PHP group test in PHP 5 and the loop iterated
> > just fine.
> >
>
> That's what it looks like. If it's not a bug, then by definition it's a
> documented "feature" in PHP 4.3x, and it'll be somewhere on the site.
>
> One of the many unfortunate aspects of a bug is that the group can't
> help you, other than to suggest you upgrade your version of PHP.
>
I realize this, which is probably it's an undocumented bug and will
remain that way. Someone tested this in PHP 4.3.10 and it worked
(passing array by reference and iterating through array), so perhaps
it's only in PHP 4.3.9 on back.
> > If I pass the array not-by-reference in PHP 4.3.9, it iterates just
> > fine:
>
> Another phrase for "not-by-reference" is "by value", FYI.
>
Thanx, the terminology escaped me.
> >
> > function doStuff($array) {
> > if (is_array($array) && @sizeof($array) > 0) {
> > foreach ($array as $key => $val) print_r("key = $key<P>"); // PRINTS
> > "key = username" 5 times and stops
> > }
> > }
> >
>
> Instead of passing by reference, you could pass by value and then return
> the modified version. That appears to be the best solution unless you have
> the power to install PHP5.
>
That's what I wound up doing, though if you wish to change autoglobals
like $_POST or $_GET you have to remember to make sure to set it to
equal the value returned by the function or method.
Phil
PS: Thanx for your help and insight!
> --
> Andrew Poelstra <http://www.wpsoftware.net/projects>
> To reach me by email, use `apoelstra' at the above domain.
> "Do BOTH ends of the cable need to be plugged in?" -Anon.
Navigation:
[Reply to this message]
|