how many times this for loop should run

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
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

how many times this for loop should run

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: how many times this for loop should run

Post 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
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

Re: how many times this for loop should run

Post by tawfiq »

how stupid of me not to know that :oops:

Thanks a ton
Post Reply