简体   繁体   English

PHP中的While循环条件

[英]A While Loop Condition In PHP

I have seen the following while loop condition in many examples and even I have used it in many times. 我在许多示例中都看到了以下while循环条件,甚至我已经使用了很多次。 I know how it works and how to use it. 我知道它是如何工作的以及如何使用。 But as i code the condition here doesn't make any sense. 但是当我编写代码时,这里的条件没有任何意义。

As I see the code, the condition is like it's always true. 正如我所看到的代码,条件就像它总是正确。 Just like while(1) . 就像while(1)一样 Because to the *mysql_fetch_assoc()*, the same data is passed is passed all the time. 因为到* mysql_fetch_assoc()*,传递的数据一直传递。 So the condition is a constant. 因此条件是一个常数。

while($arr = mysql_fetch_assoc($data)) 
{
      //other code
}

Now, where Am I Wrong ???? 现在,我在哪里错了????

Each call to mysql_fetch_assoc gets the next row from the result set. 每次对mysql_fetch_assoc调用都会从结果集中获取下一行。 If there is no row anymore it returns false and the loop terminates. 如果不再有任何行,则返回false并终止循环。

$data is a resource data type and will probably keep the state about which row was fetched last. $data是一种资源数据类型,可能会保留有关最后获取哪一行的状态。
This is not so unusual, even arrays have an internal pointer to the current element which can be manipulated using certain array functions. 这并不罕见,即使数组都具有指向当前元素的内部指针,也可以使用某些数组函数对其进行操作。

From the PHP website , mysql_fetch_assoc " Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. " PHP网站上 ,mysql_fetch_assoc“ 返回与所获取的行相对应的字符串关联数组,如果没有更多行,则返回FALSE。

Therefore, whenever there are still rows available, mysql_fetch_assoc will return an associated array of the row contents, will increment the pointer to the next row and the while loop will execute. 因此,只要仍有行可用, mysql_fetch_assoc将返回行内容的关联数组,将指针增加到下一行,并执行while循环。

When there are no more rows mysql_fetch_assoc will return FALSE and the loop will break. 当没有更多的行时, mysql_fetch_assoc将返回FALSE ,并且循环将中断。

Also note that as of PHP 5.5.5 this function will be deprecated. 另请注意,自PHP 5.5.5起,此功能将被弃用。

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

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