Page 1 of 1

how many times this for loop should run

Posted: Wed Sep 09, 2009 6:17 am
by tawfiq

Code: Select all

 
for($i=1;$i<=2;$i++){
    
    if($i = 1){
         echo "<br/>we are here for you";
    }
}
 
It's running forever constantly displaying the message. I expected the msg to be displayed just once. What am I missing?

Re: how many times this for loop should run

Posted: Wed Sep 09, 2009 6:27 am
by Mark Baker

Code: Select all

if($i = 1){
is resetting the value of $i every iteration. use

Code: Select all

if($i == 1){
instead
= means assign a value to a variable
== means comparison

Re: how many times this for loop should run

Posted: Wed Sep 09, 2009 6:35 am
by tawfiq
how stupid of me not to know that :oops:

Thanks a ton