Edit report at http://bugs.php.net/bug.php?id=54652&edit=1
ID: 54652
User updated by: rdli dot data at gmail dot com
Reported by: rdli dot data at gmail dot com
Summary: Bug when switch expression is zero
Status: Bogus
Type: Bug
Package: Unknown/Other Function
PHP Version: 5.3SVN-2011-05-02 (SVN)
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-05-03 01:16:43] [email protected]
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-02 23:06:22] felipecg00 at gmail dot com
($price > 100) is false.
false is 0, then case is executed.
------------------------------------------------------------------------
[2011-05-02 22:54:12] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54652&edit=1