简体   繁体   English

在循环中PHP没有条件?

[英]No condition in PHP while loop?

So, I'm building my first super basic CMS using PHP. 所以,我正在使用PHP构建我的第一个超级基本CMS。 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. 困扰我的一件事是使用while循环来获取帖子。 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. 在我看来它所做的就是为变量$ 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 . 请不要在新代码中使用mysql_*函数 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. 了解准备好的语句 ,并使用PDOMySQLi - 本文将帮助您确定哪些。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程


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. 如果有更多结果, mysql_fetch_assoc()返回一个数组,如果没有结果,则返回false If it returns false , the condition will evaluate to false and the loop would break. 如果它返回false ,则条件将评估为false并且循环将中断。

Basicaly you are fetching new row and assigning it to $post variable with: 基本上你要获取新行并将其分配给$post变量:

while ($post = mysql_fetch_assoc($query))

mysql_fetch_assoc : 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. 返回与获取的行对应的字符串关联数组,如果没有其他行,则返回FALSE

So when mysql_fetch_assoc returns false it is assigned to $post variable and then it is evaluated within while , then loop stops. 因此,当mysql_fetch_assoc返回false它被分配给$post变量,然后在while内进行评估,然后循环停止。

As explained on the manual page for mysql_fetch_assoc : 正如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. 返回值:返回与获取的行对应的字符串关联数组,如果没有其他行,则返回FALSE。

while ($post = mysql_fetch_assoc($query))

Means first $post gets a value. 意味着第一个$ post获得一个值。 mysql_fetch_assoc returns false when there is an error. 当出现错误时,mysql_fetch_assoc返回false。

The while loop checks that $post is not false, it can be true, an integer, an array, etc. while循环检查$ post是否为false,可以为true,整数,数组等。

It's part of PHP. 它是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. 它为所有3个变量分配7 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. 当没有更多要提取的数据时,mysql_result将返回行数据数组或布尔值false。 That array/boolean is assigned to $row. 该数组/布尔值分配给$ row。 And since this assignment has the while() wrapped around it, the array/boolean propagates up to the while(). 由于这个赋值包含while(),所以array / boolean传播到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. 如果你得到一行数据,那么这是一个被while处理为布尔值的数组,并且循环继续。 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. 如果没有更多数据,mysql_fetch返回一个布尔值false,它被赋值给$ row,而false也是赋值的结果,这意味着while()得到一个FALSE,并且循环终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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