php 中的 while 循环带有赋值运算符

发布于 2024-11-19 18:03:13 字数 196 浏览 0 评论 0原文

我正在查看的代码是这样做的...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安穩 2024-11-26 18:03:13

[...S]只要 $info 被赋予 false 以外的值,这段代码就会执行?

相当,是的。即使该表达式中有赋值运算符,该表达式本身仍然代表一个值。在这种情况下,整个表达式的结果等于对 $info 的赋值。换句话说:表达式与 $info 相同表达式已分配给 $info - 最后一个变体可能是最好的描述。

因此,现在每当 $info 等于 true 时,while 内的代码块就会被执行。

请记住,比较是松散比较。因此,不仅 false 而且 NULL 或空数组都会停止内部代码块的执行。

[... S]o as long as $info gets assigned a value other than false, this code will execute?

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 to true, the code block inside while will be executed.

Keep in mind that the comparison is a loose comparison. So not only false but as well NULL or an empty array will stop the execution of the inner code-block.

要走就滚别墨迹 2024-11-26 18:03:13

对于每条记录,$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).

戏舞 2024-11-26 18:03:13

hakre 的回答很好。也就是说,它将

while ($info=mysql_fetch_array($data_jurisdiction))

相同的方式执行,

while (mysql_fetch_array($data_jurisdiction)==true)

以与此甚至此

$info = mysql_fetch_array($data_jurisdiction);
if($info==true)

因此请记住,如果 mysql_fetch_array($data_jurisdiction) 返回任何可以评估为 false 的内容,则分配将不起作用。其中一些值是(我知道我会忘记一些:

  • 0
  • "0"
  • false
  • "false"
  • NULL
  • ""
  • array() (对此不太确定)

great answer from hakre. what is said is that

while ($info=mysql_fetch_array($data_jurisdiction))

will execute in the same way as this

while (mysql_fetch_array($data_jurisdiction)==true)

or even this

$info = mysql_fetch_array($data_jurisdiction);
if($info==true)

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:

  • 0
  • "0"
  • false
  • "false"
  • NULL
  • ""
  • array() (not fully sure about this one)
倾城花音 2024-11-26 18:03:13

只要 $info 被赋予 false 以外的值,这段代码就会执行?

是的。

as long as $info gets assigned a value other than false, this code will execute?

Yes.

几味少女 2024-11-26 18:03:13

如果 $info 为 false,它会循环并停止

mysql_fetch_array(); 逐行清除,以便始终有新结果

it does loop and stops if $info is false

mysql_fetch_array(); clears row by row so there is new result alltimes

厌倦 2024-11-26 18:03:13

只要 while 表达式的计算结果为 true,while 循环就会重复执行嵌套语句。

示例中的表达式 $info = mysql_fetch_object($data_jurisdiction) 检查 $info(从 mysql_fetch_object() 分配的值)是否等于是的,在类型杂耍之后。

这里理解两件事很重要:

  1. mysql_fetch_object() 返回结果集的下一行,直到到达数据集末尾,此时返回 false。请参阅此处的方法文档
  2. 所有不等于 0 或 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 from mysql_fetch_object() is equal to true, after type juggling.

It is important to understand two things here:

  1. 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.
  2. All assigned values of variables which are not equal to 0, or null evaluate to true after type juggling.
污味仙女 2024-11-26 18:03:13

从手册:

赋值表达式的值就是被赋值的值。即“$a = 3”的值为3。

同样来自关于mysql_fetch_array的手册:

返回与所获取的行相对应的字符串数组,如果没有更多行,则返回 FALSE。

因此,一旦没有更多的行,赋值将变成:

$info = false

在 while 条件下将被评估为 false,导致循环终止。

From the manual:

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3.

Also from the manual about mysql_fetch_array:

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

Therefore, once there are no more rows, the assignment will turn into:

$info = false

Which will get evaluated as false in the while condition, causing the loop to terminate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文