简体   繁体   中英

No condition in PHP while loop?

So, I'm building my first super basic CMS using PHP. I don't want to simply copy the code from the tutorial I'm watching, but really understand it. One thing that bothers me is the use of the while loop for fetching posts. In the code below, I can't see how the statement inside within the rounded brackets constitute a condition. Seems to me all it does is assigning an array to the variable $post. How can you loop over something that is not a condition is my question, I suppose. Thanks!

function get_posts () {
   $query = mysql_query("SELECT * FROM posts") or die(mysql_error());

   while ($post = mysql_fetch_assoc($query)) {
      echo $post['Content'];
   }
}

STOP!

The tutorial you're watching is severely outdated! Please, don't use mysql_* functions in new code . They are no longer maintained and the deprecation process has begun on it. See the red box ? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial .


As for the specific question, this line

while ($post = mysql_fetch_assoc($query)) {

Is the condition. mysql_fetch_assoc() returns an array if there are more results, and false , in the case there aren't any. If it returns false , the condition will evaluate to false and the loop would break.

Basicaly you are fetching new row and assigning it to $post variable with:

while ($post = mysql_fetch_assoc($query))

mysql_fetch_assoc :

Return Values

Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

So when mysql_fetch_assoc returns false it is assigned to $post variable and then it is evaluated within while , then loop stops.

As explained on the manual page for mysql_fetch_assoc :

Return values: Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

while ($post = mysql_fetch_assoc($query))

Means first $post gets a value. mysql_fetch_assoc returns false when there is an error.

The while loop checks that $post is not false, it can be true, an integer, an array, etc.

It's part of PHP. The result of an assignment is the value being assigned. That allows things like:

$x = $y = $z = 7;

which assigns 7 to all 3 variables. As such, given

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

mysql_result will return an array of row data or a boolean false when there's no more data to fetch. That array/boolean is assigned to $row. And since this assignment has the while() wrapped around it, the array/boolean propagates up to the while().

If you get a row of data back, it's an array which is treated as a boolean true by the while, and the loop continues. If there's no more data, mysql_fetch returns a boolean false, which is assigned to $row, and that false is also the result of the assignment, meaning while() gets a FALSE, and the loop terminates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM