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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.