[PHP-DEV] PHP 4.0 Bug #9482 Updated: || operator gives unexpected results From: torben <email protected>
Date: 02/28/01

ID: 9482
Updated by: torben
Reported By: ssegarra <email protected>
Old-Status: Open
Status: Closed
Bug Type: *General Issues
Assigned To:
Comments:

This isn't a bug; it's a result of the differing precedence for || and or (which
are otherwise identical, btw).

Try this:

  $db = mysql_connect("localhost", "root") || print "error connecting";
  echo $db;

The output should be '1'. The reason is that || has higher precedence than =,
so PHP sees the above statement as:

  $db = (mysql_connect("localhost", "root") || print "error connecting");

...which means that $db is assigned the value of the expression in parens, which
in this case will always evaluate to 1, which is not a valid MySQL resource.

Next try this:

  ($db = mysql_connect("localhost", "web")) || print "error connecting ";
  echo $db;

The output should be 'Resource id #1', since the precedence has now been forced.
The other way to force precedence is to use the lower-precedence 'or', as you
mentioned.

The operator precedence table can be found in the manual at:

  http://www.php.net/manual/en/language.operators.precedence.php

Hope this helps,

Torben

Previous Comments:
---------------------------------------------------------------------------

[2001-02-27 10:52:12] ssegarra <email protected>
<?

$db = mysql_connect("localhost", "web") || print "error connecting ".mysql_error()." ".mysql_errno();
mysql_select_db("surveys");

$result = mysql_query("select * from attract") || print "error querying ".mysql_error()." ".mysql_errno();
        
$i = 0;
while ($row = mysql_fetch_array($result)){ // && ($i++ < 5)){
        echo $row[0]." ".$row[1]."<br>";
}
?>

this script will repeatedly give me invalid MySQL Link Resource Warnings, but by changing the "||" to "or" it will work fine.

Thanks

---------------------------------------------------------------------------

ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at http://bugs.php.net/?id=9482&edit=2

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>