Page 1 of 1
Problems with loops going further than they should [solved]
Posted: Thu May 24, 2007 7:54 pm
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!
Posted: Thu May 24, 2007 9:52 pm
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();
Posted: Fri May 25, 2007 2:23 pm
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
Posted: Fri May 25, 2007 3:03 pm
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.
Posted: Fri May 25, 2007 3:50 pm
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
Posted: Fri May 25, 2007 3:58 pm
by pickle
Did the loop run at all? If not, then that's an issue with the initial values, not the loop.
Posted: Fri May 25, 2007 4:51 pm
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.
Posted: Fri May 25, 2007 4:56 pm
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.
Posted: Fri May 25, 2007 7:02 pm
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++;
}
?>
Posted: Fri May 25, 2007 8:58 pm
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:
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.
Posted: Fri May 25, 2007 10:19 pm
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?
Posted: Sat May 26, 2007 10:26 am
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?
Posted: Sat May 26, 2007 11:12 am
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
I doubt this is a PHP bug, most likely the OP only posted an example and not the actual code.
Posted: Sun May 27, 2007 2:47 pm
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.
Posted: Mon May 28, 2007 8:55 pm
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.
