Re: For Loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



$i = $months_arr_length is an assignment, not a comparison, so it will always evaluate to true, and you are setting your counter var to 12 every time the loop runs, that's why it runs infinitely.


> 
> From: "R. Van Tassel" <jazzsnob@xxxxxxxxxxxxx>
> Date: 2004/12/08 Wed PM 04:41:00 EST
> To: "'PHP general'" <php-general@xxxxxxxxxxxxx>
> Subject:  For Loop
> 
> I have a question about the following code:
> 
> **********************************************
> 
> $months_arr = array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG",
> "SEP", "OCT", "NOV", "DEC");
> $months_arr_length = count($months_arr);
> 
> for($i = 0; $i = $months_arr_length; $i++){
> 	echo $months_arr[1]." <br />";
> }
> 
> ***********************************************
> 
> When I run the code above it continues and never stops. According to the PHP
> documentation the second condition works as follows:
> 
> "In the beginning of each iteration, expr2 is evaluated. If it evaluates to
> TRUE, the loop continues and the nested statement(s) are executed. If it
> evaluates to FALSE, the execution of the loop ends."
> 
> In the first iteration of the loop $i would equal 0 and the array length is
> 12. 12 is consistent. To it seems that the loop should STOP when the counter
> reaches 12, equal to the length of the array. Why is it continuing in a
> neverending loop? What am I missing?
> 
> Changing the condition of the for loop to $i <= $months_arr_length; fixes
> everything and it works properly.
> 
> Can someone please explain where my confusion is?
> 
> Thanks,
> ~R. Van Tassel
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux