php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54652 Bug when switch expression is zero
Submitted: 2011-05-02 22:54 UTC Modified: 2011-05-03 02:36 UTC
From: rdli dot data at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3SVN-2011-05-02 (SVN) OS:
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: rdli dot data at gmail dot com
New email:
PHP Version: OS:

 

 [2011-05-02 22:54 UTC] rdli dot data at gmail dot com
Description:
------------
//execute function parameter with $price = 0

myPrice(0);



Test script:
---------------
function myPrice($price = 50 ){
switch ($price) {
   case ($price > 100) :
   echo $price, '<br />';
   echo "Price is $100 up.";
   break;

   case ($price > 50) :
   echo "Prince is $50 up.";
   break;

   case ($price >25):
   echo "Prince is $25 up.";
   break;

   default:
   echo "Prince is no more then $25.";
   break;

   }
}


Expected result:
----------------
Prince is no more then $25.

Actual result:
--------------
0
Price is $100 up.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-02 23:06 UTC] felipecg00 at gmail dot com
($price > 100) is false.
false is 0, then case is executed.
 [2011-05-03 01:16 UTC] pierrick@php.net
-Status: Open +Status: Bogus
 [2011-05-03 01:16 UTC] pierrick@php.net
You need to replace switch($price) by switch(true) so that the first case with an 
expression evaluated to true will be executed.

http://fr.php.net/manual/en/control-structures.switch.php
 [2011-05-03 02:23 UTC] rdli dot data at gmail dot com
function myPrice($price){
switch (true) {
	case ($price > 100) :
		echo '$price = ', $price, '<br />';
		echo "Price is $100 up.";
		break;

	case (($price > 50) || ($price =50)):
	        echo '$price = ', $price, '<br />';
		echo "Price is $50 and up.";
		break;

	case ($price >25):
		echo "Price is $25 up.";
		break;

	default:
		echo "Price is no more then $25.";
		break;

	}
}

When parameter $price is less than 50, for example 30, execute myPrice(30), 
actual result always is:

$price = 50
Price is $50 and up.
 [2011-05-03 02:36 UTC] rasmus@php.net
Because you have $price=50 there instead of $price==50
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 10:01:27 2025 UTC