php 中的 while 循环带有赋值运算符
我正在查看的代码是这样做的...
while ($info=mysql_fetch_array($data_jurisdiction))
{
//some stuff
}
我想知道这个 while 语句是做什么的?它里面有一个赋值运算符,所以只要 $info 被赋值为 false 以外的值,这段代码就会执行?
the code I'm looking at does this...
while ($info=mysql_fetch_array($data_jurisdiction))
{
//some stuff
}
I'm wondering what does this while statement do? it has an assignment operator within it, so as long as $info gets assigned a value other than false, this code will execute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
相当,是的。即使该表达式中有赋值运算符,该表达式本身仍然代表一个值。在这种情况下,整个表达式的结果等于对
$info
的赋值。换句话说:表达式与$info
相同或表达式已分配给$info
- 最后一个变体可能是最好的描述。因此,现在每当
$info
等于true
时,while
内的代码块就会被执行。请记住,比较是松散比较。因此,不仅
false
而且NULL
或空数组都会停止内部代码块的执行。Quite, yes. Even there is an assignment operator within that expression, the expression itself still stands for a value. In this case the result of the whole expression is equal to the assignment to
$info
. In other words: The expression is the same as$info
or the expression has been assigned to$info
- the last variant is perhaps the best description.So now whenever
$info
equals totrue
, the code block insidewhile
will be executed.Keep in mind that the comparison is a loose comparison. So not only
false
but as wellNULL
or an empty array will stop the execution of the inner code-block.对于每条记录,
$info
都将填充当前行,直到到达结果集的末尾,此时它将被设置为 false(这应该停止 while 循环)。For each record
$info
will be populated with the current row, until it reaches the end of the result set when it will be set to false (which should stop the while loop).hakre 的回答很好。也就是说,它将
相同的方式执行,
以与此甚至此
因此请记住,如果 mysql_fetch_array($data_jurisdiction) 返回任何可以评估为 false 的内容,则分配将不起作用。其中一些值是(我知道我会忘记一些:
great answer from hakre. what is said is that
will execute in the same way as this
or even this
so keep in mind that if mysql_fetch_array($data_jurisdiction) returns anything that can be evaluated to false, the assignment won't work. some of those values are (and I know I will forget a few:
只要 $info 被赋予 false 以外的值,这段代码就会执行?
是的。
as long as $info gets assigned a value other than false, this code will execute?
Yes.
如果
$info
为 false,它会循环并停止mysql_fetch_array();
逐行清除,以便始终有新结果it does loop and stops if
$info
is falsemysql_fetch_array();
clears row by row so there is new result alltimes只要 while 表达式的计算结果为
true
,while 循环就会重复执行嵌套语句。示例中的表达式
$info = mysql_fetch_object($data_jurisdiction)
检查$info
(从mysql_fetch_object()
分配的值)是否等于是的,在类型杂耍之后。这里理解两件事很重要:
mysql_fetch_object()
返回结果集的下一行,直到到达数据集末尾,此时返回 false。请参阅此处的方法文档。null
的变量赋值在类型调整后都会计算为 true。A while loop will execute the nested statement(s) repeatedly, as long as the while expression evaluates to
true
.The expression in your example
$info = mysql_fetch_object($data_jurisdiction)
checks whether the$info
, the assigned value frommysql_fetch_object()
is equal to true, after type juggling.It is important to understand two things here:
mysql_fetch_object()
returns the next row of the result set until the end of the data set is reached, where it returns false. See the method documentation here.null
evaluate to true after type juggling.从手册:
同样来自关于mysql_fetch_array的手册:
因此,一旦没有更多的行,赋值将变成:
在 while 条件下将被评估为 false,导致循环终止。
From the manual:
Also from the manual about mysql_fetch_array:
Therefore, once there are no more rows, the assignment will turn into:
Which will get evaluated as false in the while condition, causing the loop to terminate.