Problems with loops going further than they should [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Problems with loops going further than they should [solved]

Post by smudge »

Hi everyone. I've got this code:

Code: Select all

for($i=$starton;$i<$days_wk1;$i++){
  echo $disp_formatted[$day_count];
  $day_count++;
}
for whatever reason, it seems to run indefinitely, but it still generates output. The weird thing is that firefox will keep "transferring data" as it says in the status bar, and it doesn't stop at $days_wk1. This is probably a stupid thing i'm missing (like = and ==), but until then, i'm very confused.
day_count starts at 0
starton is 1
and days_wk1 is 6
when the loop starts. All the values are correct, except for the fact that it doesn't stop. Help would be greatly appreciated!
Last edited by smudge on Mon May 28, 2007 8:55 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);
echo "<div>Debug: for($i=$starton;$i<$days_wk1;$i++){</div>\n"; flush();
for($i=$starton;$i<$days_wk1;$i++){
  echo '<div>Debug: i=', $i, '  day_count=', $day_count,"</div>\n";
  echo $disp_formatted[$day_count];
  $day_count++;
}
echo "<div>Debug: } </div>\n"; flush();
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

I'm still not getting any clues as to why it's doing this, but it's printing this with your debugging code:

Code: Select all

Debug: for(1= 1 ;1<6;1++){
Debug: i= 1 day_count=0
Debug: i= 1 day_count=1
Debug: i= 1 day_count=2
Debug: i= 1 day_count=3
...
The page still doesn't stop loading, i is always 1, and day_count goes to 31; I don't know if it's coincidence, but there are 31 entries in disp_formatted.

I've tried changing i to other variables and unsetting i before the loop, but it didn't have an effect
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Pretty simple, but try this to help debug:

Code: Select all

for($i=$starton;$i<$days_wk1;$i++){
echo '<br /><br />$starton: '.$starton;
echo '<br />$days_wk1: '.$days_wk1;
echo '<br />$i: '.$i

  //echo $disp_formatted[$day_count];
  //$day_count++;
}
Might give you more insight as to what's going on.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

That didn't even output anything - at all!

So far I have 2 possible diagnosises (right word?):
-Microsoft (somehow it is, even though server is Linux)
-Broken PHP

for 1 symptom:
-infinite loop via variable not changing
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Did the loop run at all? If not, then that's an issue with the initial values, not the loop.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Hard to tell, since I added echos before and after the loop, which indicates that the code stops and fails, yet interestingly enough, browser keeps loading indefinitely. if it helps, server is running php 5.0.4 with most if not all of the default modules on fedora. probably not though. I've racked my brain for answers, and im not coming up w/ any. could it be because i is already assigned to? I would say no because it gets a new definition in the loop.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

To be honest, in funky situations like this, with such simple code & after I've wracked my brain, I just delete the offending code & retype from scratch. That's fixed the problem more times than it should.

Don't just copy & paste or re-type what you've got there, delete the for loop entirely & re-code it, re-thinking up the logic & everything. You might come to the (seemingly) exact same solution, but it might also get rid of the problem.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There is a parse error in that loop code pickle posted. It needs a semicolon at the end of the last echo line.

Do this for me will ya?

Code: Select all

<?php
echo 'Start on is ' . $starton . '<br />';
echo 'Days Week 1 is ' . $days_wk1 . '<br />';

for ($i = $starton; $i < $days_wk1; $i++) {
  echo 'Counter is at ' . $i . '<br />';
  //echo $disp_formatted[$day_count];
  //$day_count++;
}
?>
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Interesting...
Everah, I tried your code, and it still did nothing. On a hunch, I checked the source code.
Where the program was outputting, it showed:

Code: Select all

Counter is at 1<br />
repeating endlessly. Endlessly as in I couldn't get to the bottom of the code. (No wonder it crashed firefox! I thought it was a fluke!)

Also, I had caught the missing ; after I pasted his code, and it wasn't the problem.

Just to double check, I ran the original again, and it still went on forever.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

<?php
echo 'Start on is ' . $starton . '<br />';
echo 'Days Week 1 is ' . $days_wk1 . '<br />'; 
Comment out the loop, and run that. What does it return?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hm, $i++ of the loop isn't executed.
Is the first code snippet you've posted really the whole story? Nothing else in the loop body?

what does

Code: Select all

<?php
echo 'version: ', phpversion(), "<br />\n";
echo 'sapi: ', php_sapi_name(), "<br />\n";
print? And where did you downoad this version of php?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

LOL, yeah I wondered why people were still asking for other snippets of code to be run after that result where $i was never incremented :lol:

I doubt this is a PHP bug, most likely the OP only posted an example and not the actual code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The reason I asked my question was because I was thinking that maybe one of the vars was not set to anything, so there was somehow an evaluation to true in there even though there was nothing to increment. It would be helpful to see what the two params evaluated to before hitting the loop.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Sorry for the wait, guys. Haven't been near a computer all weekend, but after some vigorous hand-debugging (I *REALLY* don't like doing that), i finally figured out the problem. Like I said in the original post, it was something very, VERY stupid. $starton is read from a text file containing lots of data, and I forgot to convert it to an integer. Like I said, stupid. I had briefly considered it before, but I threw it out, because in the loop before, I had $i>$starton, and because starton was 1, it ran once, and I didn't think twice about it.

BTW: the loop I first posted came as-is from my program, no examples.

Thanks a bunch to all those who came to my aid. :D
Post Reply