php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #4037 improper parsing of logical statements
Submitted: 2000-04-04 18:52 UTC Modified: 2000-08-15 10:10 UTC
From: oded at geek dot co dot il Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0 Release Candidate 1 OS: Linux 2.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oded at geek dot co dot il
New email:
PHP Version: OS:

 

 [2000-04-04 18:52 UTC] oded at geek dot co dot il
it seems that PHP4RC1 doesn't evaluate logical statements in the same way as PHP3 did . this causes some errors which (at first glance) looked to me as MySQL driver errors where a valid SQL statement, when implemented in a code similar to this : 
if ($result = mysql_query($query) && $row = mysql_fetch_array($result)) { 
would return a mysql warning : "Supplied argument is not a valid MySQL result ersource" on the mysql_fetch_array command. 
I think this happens because PHP4 doesn't evaluate the mysql_query part before the mysql_fetch_array part, which results in the fact that the $result variable isn't set in time for the mysql_fetch_array command. 
this behavior could be well demonstrated by the two following pieces of code : 
1) 
$t = 1; 
if ( $t = $t + 1 && $w = $t + 1 ) { 
echo "right ! t: $t w: $w"; 
} else { 
echo "wrong! t: $t w : $w"; 
} 

2) 
$t = 1; 
if ( ($t = $t + 1) && $w = $t + 1 ) { 
echo "right ! t: $t w: $w"; 
} else { 
echo "wrong! t: $t w : $w"; 
} 

which although they look semanticly the same give different results. 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-30 13:04 UTC] zak@php.net
I do not think that this is a bug. However, I have never been on to worry about the subtleties of operator precedence, so I may very well be wrong.

Perhaps someone who understands this better can confirm, refute or lambast my explaination.

The && has left associativity.  The next operator on the left of the && is a +.  && has a higher precedence than + ... so, before $t = $t + 1 can be evaluated, the && grabs 1 for its logical test, compares it to the result of the expression on the right and returns true. Thus it skips the addition and assignment operation on the left

 [2000-08-15 10:10 UTC] waldschrott@php.net
zak?s right, this is not a bug, maybe it was in PHP3,
using () you?re forcing zend to evaluate the left part first (2), closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 24 12:01:31 2024 UTC