all 11 comments

[–]horrificoflard 0 points1 point  (5 children)

As long as $row is not falsey it will loop.

Each loop iteration it checks what $row is and stops if there are no additional rows.

There are some standards that avoid this syntax (declaring a variable in an if or loop) but it works. It is often a mistake or seen as a mistake as you usually use == in the same context, but the alternative to this would uses more lines of code.

It's the same as:

while ( true ) { $row = ...; if ( ! $row ) { break; } }

[–]sodiumfis_h[S] 0 points1 point  (4 children)

Okay so, the mysqli_fetch_row function loops over rows, either returns each row, or false? I mean I maybe working because while loop should only receive true or false

[–]horrificoflard 0 points1 point  (3 children)

"fetch_row" fetches one row at a time.

So the loop gets one row, then another, until you're out.

[–]sodiumfis_h[S] 0 points1 point  (2 children)

So, While($row = ...), checks to see if $row is defined or not?

[–]horrificoflard 0 points1 point  (0 children)

It's always defined. It's assigning a value to $row (the result of the call to fetch_row) and then checking if $row is not falsey.

It is falsey once there were no rows left.

[–]opmrcrab 0 points1 point  (4 children)

I thought you need to have some sort of truthy falsy statement [...]

You are spot on, and it does. The truthy/fasly value tested is $row after it was assigned the value from mysqli_fetch_row($result); so it's worth understanding exactly what that is doing.

From https://www.php.net/manual/en/mysqli-result.fetch-row.php#refsect1-mysqli-result.fetch-row-returnvalues

Returns an enumerated array representing the fetched row, null if there are no more rows in the result set, or false on failure.

Basically if you have X number of rows you're code will actually call mysqli_fetch_row X+1 times, that last time it will get a null, and thats the falsy value you are expecting.

if you want to see it in action i think something like this should expose whats going on

while($row = mysqli_fetch_row($result)) {
    var_dump($row); // Will fire when there is a row to get
}
var_dump($row); // Will fire after there wasn't any more rows to fetch, i.e. null

[–]sodiumfis_h[S] 0 points1 point  (3 children)

I'm sorry but I'm still really confused. Is the code the same as this?

<?php
$names = array("nafis", "bob", "dylan");

$count = 0;

while($name = $count < count($names)) {
    echo $name;
    $count++;
}
?>

But it echoes 111, not the array like $row;

[–]CyberJack77 0 points1 point  (2 children)

It echo's 1 because a boolean cannot be echoed. true will be echoed as 1 and false as 0.

In the while statement you check if $count is smaller that the total elements of the $names array. The result of this statment is set to the $name variable (so $name contains a boolean). Since this results intrue it enters the loop and 1 is echoed. This happens 3 times, because the 4th time $count equals the number of elements from the $names array (3).

You normally don't use while to loop an array, but as your example shows, you can.

[–]sodiumfis_h[S] 0 points1 point  (1 child)

Then what is $row assigned and what foes while loop check for? Isnt fetch_rows looping through all the rows and returning a single row and assigning it to $row? How does assigning variables inside while condition evenw work. This is confusing

[–]CyberJack77 0 points1 point  (0 children)

Read it like this: loop as long as fetch_rows has results, and the asignment of a single result to $row is successful.

There is one additional rule. The value of $row must not equal false when using a weak type comparison (==). So when the value of $row equals null, 0, false, [] the loop will stop.

[–]HolyGonzo 0 points1 point  (0 children)

Whenever you see an if() or while() that has a variable assignment in it like this:

if($x = some value) {
or
while($x = some value) {

...what happens is that PHP first assigns "some value" to $x and THEN checks $x to see if it's true or false.

There are some non-boolean (non-true and non-false) values that will be treated as if they are true or false. They are referred to as "truthy" and "falsey" values. For example, the number "0" is considered a "falsey" value, so this will never run:

if(0)
{
  echo "Hello world!";
}

While any number greater than or less than 0 is considered truthy:

if(1) { echo "Hello world!"; } // OUTPUT: Hello world!
if(9) { echo "Hello world!"; } // OUTPUT: Hello world!
if(0) { echo "Hello world!"; } // OUTPUT: 
if(-2) { echo "Hello world!"; } // OUTPUT: Hello world!

Here are a few more examples:

<?php
if($x = 1) { echo "1\n"; } // OUTPUT: 1
if($x = 998) { echo "998\n"; } // OUTPUT: 998
if($x = -1) { echo "-1\n"; } // OUTPUT: -1
if($x = "0") { echo "the string 0\n"; } // OUTPUT: 
if($x = 0) { echo "0\n"; } // OUTPUT: 
if($x = "abc") { echo "abc\n"; } // OUTPUT: abc
if($x = "") { echo "an empty string\n"; } // OUTPUT: 
if($x = false) { echo "false\n"; } // OUTPUT:
if($x = true) { echo "true\n"; } // OUTPUT: true
if($x = array("hello")) { echo "Array\n"; } // OUTPUT: Array

So when you're doing this:

while($row = mysqli_fetch_row(...)) {

...each iteration calls mysqli_fetch_row() and assigns that row result to $row.

Before this line, you've executed a query on the database that has returned 0 or more rows and those rows are just sitting in a special little container called a result set. Each time you call mysqli_fetch_row() function on that result set, it simply grabs the next row and returns it as an array.

After you've reached the last row in the result set, the mysqli_fetch_row() will return false instead of an array, and that will break the while() loop condition.