为什么这个循环在使用 && 时返回 1而不是“和”?
如果 PHP &&
运算符被使用,在 while($row = mysql_fetch_assoc($result) and $runningOK) 循环中猜谜语... 和
的结果是 mysql_fetch_assoc
严重失败,运行时仅返回数字 1
。
我已经尝试了 mysql_fetch_array() 并就位,但仍然遇到 1
问题。当且仅当我将 &&
替换为 and
(如当前 while
语句)时,才会返回正确的行。
我在之前、内部和之后放置了调试语句以确保这一点。我想知道这是 PHP 的怪癖还是我无法解释的东西。
// Query
$selectQuery = "SELECT * FROM jobs_cache LIMIT 20";
// Run the Selection Query.
$result = mysql_query($selectQuery)
or die('Query Failed: '.mysql_error());
// Loop through results.
$runningOK = TRUE;
$resubmitList = array();
while($row = mysql_fetch_assoc($result) and $runningOK)
{
// Resubmit The Job
try
{
$client->addTaskBackground($row['function_name'],$row['job_data']);
$resubmitList[] = (string)$row['job_cache_id'];
}
catch(Exception $e)
{
echo "Error adding task for job id: " . $row['job_cache_id'];
$runningOK = FALSE;
}
}
Riddle me this... in the while($row = mysql_fetch_assoc($result) and $runningOK)
loop, if the PHP &&
operator is used in place of the and
then mysql_fetch_assoc
fails terribly and returns just the number 1
when running.
I've tried mysql_fetch_array()
and in place and I still have the 1
problem. It is when, and only when, I replace the &&
with an and
like the current while
statement that the correct rows are returned.
I had placed debug statements before, inside, and after to insure this. I would like to know if this is a PHP quirk or something I couldn't account for.
// Query
$selectQuery = "SELECT * FROM jobs_cache LIMIT 20";
// Run the Selection Query.
$result = mysql_query($selectQuery)
or die('Query Failed: '.mysql_error());
// Loop through results.
$runningOK = TRUE;
$resubmitList = array();
while($row = mysql_fetch_assoc($result) and $runningOK)
{
// Resubmit The Job
try
{
$client->addTaskBackground($row['function_name'],$row['job_data']);
$resubmitList[] = (string)$row['job_cache_id'];
}
catch(Exception $e)
{
echo "Error adding task for job id: " . $row['job_cache_id'];
$runningOK = FALSE;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是由于运算符优先级所致。有关详细信息,请参阅手册...这是相关部分。
因此,使用 &&,表达式的结果被分配为 true,其计算结果为 1。使用 and,它计算结果为 mysql 函数的结果 - 我认为更多你想要的。
顺便说一下,您还可以使用
break
来消除对 $runningOK 变量的需要。为此,请从while
条件中删除和 $runningOK
并将$runningOK = FALSE;
更改为break;
如果 catch 块被触发,它将结束循环。It's due to operator precedence. See the manual for details... here's the relevant part.
So, with the &&, the result of the expression is assigned to true, which evaluates to 1. With and, it evaluates to the result of the mysql function - more what you want, I reckon.
By the way, you could also use
break
to eliminate the need for the $runningOK variable. To do so, removeand $runningOK
from thewhile
condition and change$runningOK = FALSE;
tobreak;
and it will end the loop if the catch block is triggered.试试这个:
可能会出现混淆,因为它正在将
mysql_fetch_assoc($result) 和 $runningOK
评估为 TRUE,然后将其分配给$row
,在此case 与1
相同如果添加括号,您很可能可以使用
&&
或and
,因为您将得到正确区分这两个评价。我确信这是替代语法的副作用。
Try this:
There might be a mix up, as it is evaluating
mysql_fetch_assoc($result) and $runningOK
to TRUE, which it then assigns to$row
, which in this case is the same as1
If you add the parenthesis, you most likely can use either
&&
orand
, as you will have properly separated the two evaluations.This is the side effect of alternative syntaxes I'm sure.
赋值运算符
=
和两个逻辑运算符&&
和AND
有不同优先级:&&
在=
之前执行,它又在AND
之前执行所以基本上语句:
等于:
而语句:
等于:
在最后一种情况下,您只是分配值 1 (如果 mysql_fetch_assoc 返回一个值)或0。
The assignment operator
=
and the two logical operators&&
andAND
have different precedence:&&
is executed before=
, which in turn is executed beforeAND
So basically the statement:
is equal to:
while the statement:
is equal to:
In the last case you are just assigning values 1 (if mysql_fetch_assoc returns a value) or 0.
这是因为运算符解决了优先级问题。
查看此表以了解:
http://php.net/manual/en /language.operators.precedence.php
当你不知道它是如何解析的时候,尽可能多地使用 () 。
It's because the operator resolving prescedence.
Check out this table to understand:
http://php.net/manual/en/language.operators.precedence.php
Use () as much as you need when you don't know how it's resolved.