MadnessRed Posted January 6, 2009 Share Posted January 6, 2009 Hi, I would like to know what the correct way to run a while loop is. At the moment my code says while ($row = mysql_fetch_array($query)) { And when I look on php.net that is the syntax they use. However when I use the Zend Studio analise code it says "Assignment in Condition", which from what I can tell means I should put... while ($row == mysql_fetch_array($query)) { Now when I think about it, that makes more sense, as a while loops "if" a condition is true, if $a is equal to $b. So basically which of these are correct? while($a == $b) while($a = $b) while($row == mysql_fetch_array($query)){ while($row = mysql_fetch_array($query)){ Quote Link to comment https://forums.phpfreaks.com/topic/139758-solved-correct-syntax-for-while-loop-zend-disagrees/ Share on other sites More sharing options...
darkfreaks Posted January 6, 2009 Share Posted January 6, 2009 i usually run with: while($row = mysql_fetch_array($query)){ Quote Link to comment https://forums.phpfreaks.com/topic/139758-solved-correct-syntax-for-while-loop-zend-disagrees/#findComment-731186 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 In the format of the array fetching it should be... while($row = mysql_fetch_array($query)){ If you are however checking if one value equals another, such as... $i=0; $x=0; while ($i == $x) { $i++; echo "Should execute once."; } You would use the == operator. Reasoning, the = is an assignment operator. So while you are allowed to assign a value to $row loop. Where as if you switched it to == (equal checker) the loop will never run cause $row probably does not equal the returned value of fetch_array. Quote Link to comment https://forums.phpfreaks.com/topic/139758-solved-correct-syntax-for-while-loop-zend-disagrees/#findComment-731189 Share on other sites More sharing options...
DarkWater Posted January 6, 2009 Share Posted January 6, 2009 The logic you're going for absolutely needs to use one equals sign. To shut Zend up, you can try: while (FALSE !== ($row = mysql_fetch_assoc($query))) { But then your code will start looking like C. Quote Link to comment https://forums.phpfreaks.com/topic/139758-solved-correct-syntax-for-while-loop-zend-disagrees/#findComment-731190 Share on other sites More sharing options...
MadnessRed Posted January 7, 2009 Author Share Posted January 7, 2009 The logic you're going for absolutely needs to use one equals sign. To shut Zend up, you can try: while (FALSE !== ($row = mysql_fetch_assoc($query))) { But then your code will start looking like C. ok, good, many thanks for that. So while($a == $b) is because its as check, is a equal to b. while $row = mysqly... is because you are assigning mysql.. to $row ok, many thanks, now I can rest assured that my code is correct Quote Link to comment https://forums.phpfreaks.com/topic/139758-solved-correct-syntax-for-while-loop-zend-disagrees/#findComment-731788 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.