Bug #76927 [Com]: Assignments in "if" conditions

From: Date: Mon, 24 Sep 2018 16:09:04 +0000
Subject: Bug #76927 [Com]: Assignments in "if" conditions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217217@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76927&edit=1 ID: 76927 Comment by: spam2 at rhsoft dot net Reported by: davide dot spagnoli at adam-italia dot it Summary: Assignments in "if" conditions Status: Open Type: Bug Package: *General Issues Operating System: CentOS Linux release 7.5.1804 (C PHP Version: 7.1.22 Block user comment: N Private report: N New Comment: nonsense! if($j=1 && $j++==1 && $j==2) a logical "and" operates from left to right and since $j is undefined and hence not 1 nothing else is evaluated (besides that $j++==1 is a terrible construct) can you tell one logical reason why anything after && should be evaluated instead stopped after the first not matching condition? that's how you optimize for performance: order the cheapest and most likely hitting stuff left in your && series and the expensive ones like function calls right Previous Comments: ------------------------------------------------------------------------ [2018-09-24 15:49:14] davide dot spagnoli at adam-italia dot it Description: ------------ In a set of conditions evaluated by an "if" block, an assignment is not recognized by next conditions. Test script: --------------- $i=1; if($i++==1 && $i==2){ echo "I'm in"; } // output "I'm in" if($j=1 && $j++==1 && $j==2){ echo "I'm in again"; } // notice "Undefined variable: j" and no output Expected result: ---------------- output: "I'm in" output: "I'm in again" Actual result: -------------- output: "I'm in" notice "Undefined variable: j" and no output ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76927&edit=1

« previous php.bugs (#217217) next »