Someone please explain how and why this loop iterates and terminates properly.

$res_array = array();

for ($count=0; $row = $result->fetch_assoc(); $count++) 
{
$res_array[$count] = $row;
}

    All for() loops can be written as while() loops, so this:

    for ($count=0; $row = $result->fetch_assoc(); $count++) 
    {
    $res_array[$count] = $row;
    }

    is equivalent to this:

    $count = 0;
    while($row = $result->fetch_assoc())
    {
        $res_array[$count] = $row;
    
    $count++;
    }

    However, there's no reason to keep track of $count at all, so I would probably have written the loop like so:

    while($row = $result->fetch_assoc())
        $res_array[] = $row;

      I understand loops, I just couldn't understand why $result->fetch_assoc() was able to terminate the loop. When there are no more rows, it appears to return a NULL.

        sosothirsty;10989247 wrote:

        I understand loops, I just couldn't understand why $result->fetch_assoc() was able to terminate the loop. When there are no more rows, it appears to return a NULL.

        It returns FALSE if there are no more rows.

          Bonesnap;10989287 wrote:

          It returns FALSE if there are no more rows.

          How do you know this? Your psychic powers are much more in tune than mine, because I can't even tell what type of object $result is, let alone what value one of its methods returns.

          For example, if we're talking about [man]MySQLi[/man], MySQLi_Result::fetch_assoc() (man page: [man]mysqli-result.fetch-assoc[/man]) does indeed return NULL when no more rows exist in the result set.

          Regardless, neither NULL nor FALSE are equivalent to boolean TRUE, so either value in a loop's conditional expression would terminate said loop.

            I was just about to post that. It does return NULL when empty. Loops terminate on FALSE and NULL indeed test as false in PHP just like it does if the value was zero. It made no sense to me that a NULL value would be treated as such.

            That's the logic I was looking for from this, please correct me if I'm wrong. Thanks for the discussion.

              8 days later
              bradgrafelman;10989291 wrote:

              How do you know this? Your psychic powers are much more in tune than mine, because I can't even tell what type of object $result is, let alone what value one of its methods returns.

              For example, if we're talking about [man]MySQLi[/man], MySQLi_Result::fetch_assoc() (man page: [man]mysqli-result.fetch-assoc[/man]) does indeed return NULL when no more rows exist in the result set.

              Regardless, neither NULL nor FALSE are equivalent to boolean TRUE, so either value in a loop's conditional expression would terminate said loop.

              I misread it. I thought he wrote mysql_fetch_assoc. :o

                In any case, I'd recommend using the while() loop in this case, so that you don't waste time/resources on the $count variable and its incrementing (though admittedly we're talking tiny amounts in the world of modern computing).

                  5 days later

                  Hi... I am also new out here.... I want to know that what is this forum all about and what are we supposed to post in here...!

                    Write a Reply...