By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

Finite $_POST loops infinitely - help!

P: n/a
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) {
print_r("sizeof(" . vname($array) . ") = " . sizeof($array) .
"<P>");
$index = 1;
foreach ($array as $key =$val) {
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

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 4

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 5

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 6

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 7

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 8

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 9

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

index = 10

key = username and val = phillip and this->data = phillip and array
name = _POST

this->data = phillip

....// and so on and so on.. as high as 200,000 at times and still
doesn't quit!!

Why is this happening, I honestly can't see why.

Thanx
Phil

Aug 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
function &array_scan(&$array) {
if (is_array($array) && @sizeof($array) 0) {
print_r("sizeof(" . vname($array) . ") = " . sizeof($array) .
"<P>");

your looping for any number higher than zero.

Flamer.

Aug 22 '06 #2

P: n/a
HUH??

Phil

flamer di******@hotmail.com wrote:
function &array_scan(&$array) {
if (is_array($array) && @sizeof($array) 0) {
print_r("sizeof(" . vname($array) . ") = " . sizeof($array) .
"<P>");


your looping for any number higher than zero.

Flamer.
Aug 22 '06 #3

P: n/a
"comp.lang.php" <ph**************@gmail.comwrites:
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.

--
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.
Aug 22 '06 #4

P: n/a

Andrew Poelstra wrote:
"comp.lang.php" <ph**************@gmail.comwrites:
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.
Aug 22 '06 #5

P: n/a
"comp.lang.php" <ph**************@gmail.comwrites:
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.
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.
>
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.

--
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.
Aug 22 '06 #6

P: n/a

Andrew Poelstra wrote:
"comp.lang.php" <ph**************@gmail.comwrites:
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.
Aug 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.