Jump to content

[SOLVED] Correct syntax for while loop? Zend disagrees.


MadnessRed

Recommended Posts

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)){

 

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.

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.