Bug #77699 [Opn->Nab]: Switch statement in foreach loop incorrect comparisons

From: Date: Tue, 05 Mar 2019 21:43:32 +0000
Subject: Bug #77699 [Opn->Nab]: Switch statement in foreach loop incorrect comparisons
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219854@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77699&edit=1 ID: 77699 Updated by: requinix@php.net Reported by: eguthrie3214 at gmail dot com Summary: Switch statement in foreach loop incorrect comparisons -Status: Open +Status: Not a bug Type: Bug Package: Arrays related Operating System: All PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php switch ($X) { case $Y: is equivalent to if ( ($X) == ($Y) ) Since you wrote switch ($i) { case $i > 0: you will get if ( ($i) == ($i > 0) ) Previous Comments: ------------------------------------------------------------------------ [2019-03-05 21:42:03] eguthrie3214 at gmail dot com Description: ------------ When using a switch statement inside a foreach loop array elements that have a value of 0 are compared in case statements incorrectly. Test script: --------------- $a = [0,0,0,0,0]; foreach ($a as $i) { switch ($i) { case $i > 0: echo 'true'; } } Expected result: ---------------- We should expect no output since 0 is not greater than 0 and therefore doesn't satisfy the case. This has been tested on both 7.2 and 7.1. Actual result: -------------- truetruetruetruetrue ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77699&edit=1

« previous php.bugs (#219854) next »