English 中文(简体)
Why does this loop return 1 when using && instead of and ?
原标题:

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;
    }
}
最佳回答

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.

问题回答

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.

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.

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签