2

I don't understand why this code works:

while ($row = mysqli_fetch_object($result)){
    echo $row->id . " ";
    echo $row->first_name . " ";
    echo $row->last_name . "<br>";
}

This is a part of a tutorial on CRUD that I am learning. The while loop goes through the whole table and lists all the rows and columns. But from my understanding of while-loops, this shouldn't work.

My guess would be that it should try to print out the current row infinite times and crash my browser as there is no change in the condition. Why does it go through all the rows? Why does it stop? I can't find answer to this anywhere.

Would there be anyone kind enough to explain in simple terms?

4
  • 2
    Each fetch gets a row and succeeds which evaluates to true so the while loop continues. When there are no more rows available to fetch the fetch returns false and the while loop ends. Commented Dec 13, 2018 at 13:56
  • 1
    @Dave This should be posted as an answer not a comment Commented Jul 28, 2019 at 22:42
  • Can you explain why you don't understand that code? Commented Jul 29, 2019 at 8:57
  • Possible duplicate of while loop in php with assignment operator Commented Jul 29, 2019 at 9:18

2 Answers 2

3

Each fetch gets a row and succeeds which evaluates to true so the while loop continues. When there are no more rows available to fetch the fetch returns false and the while loop ends.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that PHP internally uses pointers. Every time a result is fetched, the pointer moves forward in the result set.
1

You assume that the condition does not change. That's where you are wrong. You can reassign values inside the while condition.

while ($row = mysqli_fetch_object($result))

mysqli_fetch_object's result gets stored in the $row variable on each iteration. The $row variable will be evaluated as a boolean, and if $row is an object, it will be truthy and hence go inside the loop body.

So on the first iteration, you'll get the first $row, then the second, and so on.

If there are no more rows mysqli_fetch_object will return null. Null will be stored in the $row variable and evaluated. Null is a falsely value and hence the loop will break.

You are most likely misreading the condition in your brain as either:

  • $row == mysqli_fetch_object($result)
  • $row === mysqli_fetch_object($result)

This changes the meaning completely.


Sidenote: Reassignment in conditions is something that php allows at different places as well and may lead to hard-to-debug bugs.

E.g. if ($bool = false) { echo "I never will be printed";} is valid php and will always lead to a false check and hence never execute the true-branch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.