PHP 4.0 Bug #9482 Updated: || operator gives unexpected results

From: Date: Wed, 28 Feb 2001 22:05:24 +0000
Subject: PHP 4.0 Bug #9482 Updated: || operator gives unexpected results
Groups: php.dev 
Request: Send a blank email to php-dev+get-47689@lists.php.net to get a copy of this message
ID: 9482 Updated by: torben Reported By: ssegarra@mitre.org 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@mitre.org <? $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

« previous php.dev (#47689) next »