php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70706 Doesn't evaluate 0
Submitted: 2015-10-13 04:16 UTC Modified: 2015-10-13 04:26 UTC
From: raquilesc at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.30 OS: Linux
Private report: No CVE-ID: None
 [2015-10-13 04:16 UTC] raquilesc at gmail dot com
Description:
------------
This code fails, 0 gets ignored and is catch by the 2nd case instead of the first one. Basically indicating than 0 is greater that 69. To evaluate a lower value you need to set it up to 1.

$a = 0;

switch ($a){
  case ($a <= 69) : echo "Less than or equal 69"; break;
  case ($a > 69 && $a < 80)  : echo "greater than 69"      ; break;
  case ($a >= 80) : echo "greater or equal 80"  ; break;
}

Test script:
---------------
This code fails, 0 gets ignored and is catch by the 2nd case instead of the first one. Basically indicating than 0 is greater that 69. To evaluate a lower value you need to set it up to 1.

$a = 0;

switch ($a){
  case ($a <= 69) : echo "Less than or equal 69"; break;
  case ($a > 69 && $a < 80)  : echo "greater than 69"      ; break;
  case ($a >= 80) : echo "greater or equal 80"  ; break;
}

Expected result:
----------------
It should evaluate 0 as a value and it is not. 





Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-13 04:26 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: *General Issues +Package: Scripting Engine problem
 [2015-10-13 04:26 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

http://php.net/manual/en/control-structures.switch.php

  switch (X) { case Y: }
is equivalent to
  if (X == Y) {
thus your
  switch ($a) { case ($a <= 69): }
is equivalent to
  if ($a == ($a <= 69)) {

Try switch (true) instead.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 13:00:02 2026 UTC