473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Finite $_POST loops infinitely - help!

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
6 1368
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
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
"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

109
by: Neo | last post by:
Hi Folks,http://www.abarnett.demon.co.uk/tutorial.html#FASTFOR Page states:for( i=0; i<10; i++){ ... }i loops through the values 0,1,2,3,4,5,6,7,8,9 If you don't care about the order of the loop...
2
by: Toli | last post by:
I have a problem with threading in vb.net, it seems if a dll freezes or falls into an infinite loop, my multithreaded program starts allocating more threads because it cant use the frozen thread ...
3
by: Babak | last post by:
Hello everyone, I'm working on some Finite Elements(FE) codes in C and now I encountered some problems in assembly stage. The main idea is that a large number of 3 by 3 elemental stiffness...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.