You are here: Re: Finite $_POST loops infinitely - help! « PHP Programming Language « IT news, forums, messages
Re: Finite $_POST loops infinitely - help!

Posted by comp.lang.php on 08/22/06 18:20

Andrew Poelstra wrote:
> "comp.lang.php" <phillip.s.powell@gmail.com> writes:
>
> > I have no idea why this is happening and I need someone to explain this
> > to me at the simplest level absolutely possible (pretend I'm a 10-year
> > old and explain it that way, please!)
> >
> > This class method:
> >
> > PHP Code:
> > /**
> > * Perform an array scan
> > *
> > * @access private
> > * @param array $array
> > * @see vname
> > */
> > function &array_scan(&$array) {
> > if (is_array($array) && @sizeof($array) > 0) {
>
> Instead of indenting the whole function, you may wish to just add a
> premature return and negate the test:
> if (!isarray ($array) || @sizeof ($array) == 0)
> return;
>
> > print_r("sizeof(" . vname($array) . ") = " . sizeof($array) .
> > "<P>");
> > $index = 1;
> > foreach ($array as $key => $val) {
>
> How about you just indent two more spaces after each {? This style is
> very erratic and hard to read.
>
> This appears to be your only loop; try replacing the body with something
> simple like
> echo "Key: $key<br />Value: $val<br /><br />\n";
> and see if the problem persists.
>
> > print_r("index = $index<P>"); $index++;
> > $this->setData($val);
> > print_r("key = $key and val = $val and this->data =
> > $this->data and array name = " . vname($array) . "<P>");
> > $this->scan($key, vname($array));
> > $array[$key] = $this->getData();
> > }
> > }
> > }
> >
> >
> > Constantly produces the following results:
> >
> > Quote:
> > sizeof(_POST) = 5
> >
> > index = 1
> >
> > key = username and val = phillip and this->data = phillip and array
> > name = _POST
> >
> > this->data = phillip
> >
> > index = 2
> >
> > key = username and val = phillip and this->data = phillip and array
> > name = _POST
> >
> > this->data = phillip
> >
> > index = 3
> >
> > ...// and so on and so on.. as high as 200,000 at times and still
> > doesn't quit!!
> >
>
> Is $index a reserved variable, or something that foreach() might be
> using internally? I assume not, but try renaming or removing it and
> see if that helps any.

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.

If I pass the array not-by-reference in PHP 4.3.9, it iterates just
fine:

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
}
}

===================================================================

Phil

>
> --
> 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]


Удаленная работа для программистов  •  Как заработать на 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

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