-2

I know this below statement works

for ($i= 0; $i=<10; $i++)

will output till 0 to 10 but when I write this code

for ($i=0; $i=10; $i++)

it print 10 for unlimited times... why why it not print 0 to 10... what error I have done to get the result 0 to 10 for it....

Waqas
  • 1
  • 1
  • 6
  • 1
    2nd block in a `for` loop is meant for a **condition** – Yogesh Mistry Apr 01 '17 at 06:24
  • In for loop, first 2 condition will be evaluated first time but in your case, you are reassigning the value 10 to i that's why end of the for loop it checks the condition agian, and i will be reassigned to 10 again and again. basically 2nd expression always be condition checking rather assigning. – Pramod Patil Apr 01 '17 at 06:26
  • i=10 is a assignment statement ,not a condition – Dongqing Apr 01 '17 at 06:31
  • @PranavCBalan .... but how can it will be in php == not working – Waqas Apr 01 '17 at 06:48
  • *"I know this below statement works `for ($i= 0; $i=<10; $i++)`"* No it doesn't because there's no such operator as `=<`. – Pang Apr 01 '17 at 06:59
  • Possible duplicate of [The 3 different equals](http://stackoverflow.com/questions/2063480/the-3-different-equals) – user3942918 Apr 01 '17 at 15:21

5 Answers5

3

The middle term in a for loop is the condition that says whether the loop should continue running. i=10 assigns 10 to i, and it also evaluates to the number 10, which is not zero, so it's considered true. Since the loop's condition is always true, it never stops running.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • so how can I achieve the result .. ==10 is not working but <=10 will shown till 10 ... no any other method ? – Waqas Apr 01 '17 at 06:38
  • `i==10` is false, because `i` was set to zero, so the loop doesn't run at all. What are you trying to accomplish? You already know how to write the condition so the loop goes from 0 to 10. – Wyzard Apr 01 '17 at 06:48
  • @Wyzard for($i=1; $i==10; $i++) no output ... i am very very new in php so i want to clear and learn max ways to get anything . – Waqas Apr 01 '17 at 06:50
  • 1
    You don't get output from that because the loop doesn't run, because its condition is false. What are you trying to make the loop do? It sounds like you want it go from 0 to 10, but you already know how to do that. – Wyzard Apr 01 '17 at 06:52
0

because i=10 always be true i==10 will be good

garine
  • 24
  • 1
0

The second statement in the for loop is the condition. When its not true it will leave the loop. i=10 is not a comparator. i will be set to 10 every round that loop goes and because it "works" its resulting in true. i == 10 would be a comparator but i would never be 10 in its first round.

user2659982
  • 131
  • 6
0

if $i< or =10 Will perform,if $i> 10 will break,each time $i will +1 you can set the $i begin to value or set the maximum to achieve the result that you want.

zh.ng
  • 16
0

" = " sign is an assignment operator and can't be used as conditional where as >= , == , <= such operators are conditional, so while checking for a condition you should not use " = "

please write like this below

for($i=0; $i <= 10; $i++)
{
echo $i;
} 
Atreya Rath
  • 119
  • 2
  • 6
  • agree but when I use == , blank data/output shown $i; for($i=0; $i==10; $i++) { echo"$i"; echo"
    "; }
    – Waqas Apr 01 '17 at 06:45