为什么这个循环在使用 && 时返回 1而不是“和”?

发布于 2024-08-21 01:37:38 字数 1045 浏览 2 评论 0原文

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

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

发布评论

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

评论(4

嘦怹 2024-08-28 01:37:39

这是由于运算符优先级所致。有关详细信息,请参阅手册...这是相关部分。

//“&&”优先级高于
“和”

//将表达式的结果(true && false)赋值给$g
// 行为类似于:($g = (true && false))

$g = true &&假;

// 常量 true 被赋值给 $h,然后 false 被忽略
// 行为类似于:(($h = true) 和 false)

$h = true 和 false;

var_dump($g, $h);

布尔值(假)
布尔(真)

因此,使用 &&,表达式的结果被分配为 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.

// "&&" has a greater precedence than
"and"

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))

$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)

$h = true and false;

var_dump($g, $h);

bool(false)
bool(true)

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, remove and $runningOK from the while condition and change $runningOK = FALSE; to break; and it will end the loop if the catch block is triggered.

未央 2024-08-28 01:37:39

试试这个:

while(($row = mysql_fetch_assoc($result)) and $runningOK)

可能会出现混淆,因为它正在将 mysql_fetch_assoc($result) 和 $runningOK 评估为 TRUE,然后将其分配给 $row,在此case 与 1 相同

如果添加括号,您很可能可以使用 &&and,因为您将得到正确区分这两个评价。

我确信这是替代语法的副作用。

Try this:

while(($row = mysql_fetch_assoc($result)) and $runningOK)

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 as 1

If you add the parenthesis, you most likely can use either && or and, as you will have properly separated the two evaluations.

This is the side effect of alternative syntaxes I'm sure.

三生殊途 2024-08-28 01:37:39

赋值运算符=和两个逻辑运算符&&AND不同优先级&&= 之前执行,它又在 AND 之前执行

所以基本上语句:

$row = mysql_fetch_assoc($result) AND $runningOK

等于:

($row = mysql_fetch_assoc($result)) AND $runningOK

而语句:

$row = mysql_fetch_assoc($result) && $runningOK

等于:

$row = (mysql_fetch_assoc($result) && $runningOK)

在最后一种情况下,您只是分配值 1 (如果 mysql_fetch_assoc 返回一个值)或0。

The assignment operator = and the two logical operators && and AND have different precedence: && is executed before =, which in turn is executed before AND

So basically the statement:

$row = mysql_fetch_assoc($result) AND $runningOK

is equal to:

($row = mysql_fetch_assoc($result)) AND $runningOK

while the statement:

$row = mysql_fetch_assoc($result) && $runningOK

is equal to:

$row = (mysql_fetch_assoc($result) && $runningOK)

In the last case you are just assigning values 1 (if mysql_fetch_assoc returns a value) or 0.

影子的影子 2024-08-28 01:37:39

这是因为运算符解决了优先级问题。

查看此表以了解:

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.

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