php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77699 Switch statement in foreach loop incorrect comparisons
Submitted: 2019-03-05 21:42 UTC Modified: 2019-03-05 21:43 UTC
From: eguthrie3214 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: eguthrie3214 at gmail dot com
New email:
PHP Version: OS:

 

 [2019-03-05 21:42 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-05 21:43 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-03-05 21:43 UTC] requinix@php.net
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) )
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 07:01:33 2025 UTC