Page 1 of 1

concering WHILE loops

Posted: Mon Sep 28, 2009 8:13 am
by itay
ok, so the while(x<y) structure I understand,
but I don't get the while($x=abc($y) structure.
[as in while($row=mysql_fetch_assoc($result))]

where is the condition here?

Re: concering WHILE loops

Posted: Mon Sep 28, 2009 8:23 am
by Mark Baker
while($row=mysql_fetch_assoc($result))

mysql_fetch_assoc($result) will return a false condition if there are no rows to return within $result. This will assign a value of false to $row, and effectively to the condition tested by the while. If data is returned by the call to mysql_fetch_assoc($result), then that data will be stored in $row, and assigning that data equates to a true condition for the while loop
Effectively (while $x = false)

Re: concering WHILE loops

Posted: Mon Sep 28, 2009 8:45 am
by onion2k
It's equivalent to (while $x = true) actually.

Re: concering WHILE loops

Posted: Mon Sep 28, 2009 9:24 am
by Mark Baker
onion2k wrote:It's equivalent to (while $x = true) actually.
Mental aberration

Re: concering WHILE loops

Posted: Mon Sep 28, 2009 9:34 am
by itay
16:31 (damn, just before sending this reply I refreshed the page and saw 2 more posts that closed the subject for me and made it clear... still, I don't wanna abandon all this text I wrote here - i'll post it anyway, if you don't mind) (i've added few timestamps so you don't get confused with the chronological order of this post)

16:25 // (IF THIS IS POST IS TOO LONG, YOU CAN SKIP TO THE LAST LINES, THAT'S WHERE THE SUMMARY OF MY QUESTION AND A REQUEST FOR VERIFICATION OF MY CONCLUSIONS APPEARS) //


16:00 thanks, I believe I understand it better now,

WHILE loops always respondes to true/false.

it checks the condition it recieves. even simple condition like (x<7) (yes or no)
of yes then sababa it operates, finish to run commands in the bracket, and go back to the statement to check the condition again. and also in more complicated like the mysql_fetch_assoc, it doesn't matter, because all he does is checking if true or false.

what that was confusing me, and still a bit is - is the lack of clear logic in that WHILE loop.
i mean:

ok

(1<7) is a statement, which can be true or false
also ($x==3) is a statement
but ($x=this and that), is not a statement, but a command to insert to x the value "this and that"

($row=mysql_fetch_assoc($result)) simply says to insert the $row the result of that function.
it doesn't make any statement at all, that can be checked for it's trueness or falseness.

so what does WHILE react to?

I understood that it return a false value if there is no more results. but how does it actually tells the while false?
is it like (when there are no results):
while($row=mysql_fetch_assoc($result)) == while($row=false) == while(false)
and the loop stops?

and if so, then if I put in the while paramater a boolean variable with true value, it will create
and if I put a boolean variable with a false value, it won't?


is it the basic of the while?

i want to deeply understand it, that's why i'm diggin :P

while($row=mysql_fetch_assoc($result))

how does it work?
is it like this?

(in case of 2 rows table)

while($row=mysql_fetch_assoc($res))
{
echo $row['product']
}

1. the parser goes throu the line... and reads while($row=mysql_fetch_assoc($result)
2. it activates the function,
2.1 the function returns the current row as a result
2.2 it moves the internal scanner of the database to the next row
3. $row recieves the result of the function
2. then it reaches the closing bracker of the while's condition ) , and check for TRUE/FALSE
3. it gets a TRUE, because the $row contatins data
4.it enters the {
5.it display the 'product' column of the first row
6. it reaches the while's closing bracket of the while's commands }, which signs him that it's time to go check condition again
7. the parser goes back to while($row=mysql_fetch_assoc($result)
8. it reaches the function, and activate it again
8.1 this time, the internal scanner is at row 2, and returns the result from there
8.2 it moves to the next row, which is empty
9. $row recieves the result of the function, that will be the 2nd row
10. then again it reaching to the closing bracket of while's condition,
11. it gets a true, because the $row contatins data
12. it enters the {
13. displaiying the 'product' column of the second row
14. reaches the }
15. go back to while($row=mysql_fetch_assoc($result)
16. activate the function
16.1 this time, the internal scanner is suppose to be at row 3, but there is no row 3, so it returns false
17. $row recieves FALSE
18. it reaches the closing bracket of the condition
19. now is the point where i'm not sure - it gets FALSE, because row doesn't contain any data, or positive value? why does the WHILE care what row contains? is the condition of while is that all the paramaters inside should be TRUE?
and what about this? while (x & y)?
when x is true, and y is false?
if even one parameter is false, it stops the loops? i think i'm explaining it to myself as i am writing.. lol.. ok so the WHILE expect the paramaters to contain either data, or TRUE value?

---

BOTTOM LINE:

so there are 2 ways of programming conditions for the while loop, yeah? like this

1. while (x<6) is a question.
2. while (x) is a actually also a question - it actually saying (while x==TRUE)

is this correct?


sorry for all the diggin, but I want it to be clear to me, as I learn this subject.

Re: concering WHILE loops

Posted: Mon Sep 28, 2009 3:33 pm
by peterjwest
Your conclusion is wrong, its actually much simpler. I'll explain.

In php nearly everything can be evaluated to a boolean (true or false), the following values are considered false:
NULL, FALSE, 0, '0' or '' (there may be a few others, these are the most common ones)
Everything else is considered true:
1, 2, -3, 'banana', array(1,2,3) (Basically anything that's not zero)

So you can say while($row = mysql_fetch_assoc($result)) and this has two purposes:
- it assigns the next row to $row, unless there are no more rows in which case $row is assigned NULL
- it checks whether $row is false (in this case NULL) and breaks the loop

So while mysql_fetch_assoc() keeps returning values the loop will continue.

Hope this helps, and by the way all capital text is generally regarded as bad on internet forums.

Re: concering WHILE loops

Posted: Mon Sep 28, 2009 4:02 pm
by Eran
When the loop checks the stopping condition, it casts its value to a boolean http://us3.php.net/manual/en/language.types.boolean.php

So any value that casts to a boolean 'false' will stop the loop. So basically the loop continues while($x != false)