Bug #54652 [Bgs]: Bug when switch expression is zero
| From: | rasmus@php.net | Date: | Tue, 03 May 2011 00:36:06 +0000 |
| Subject: | Bug #54652 [Bgs]: Bug when switch expression is zero | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-160519@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54652&edit=1
ID: 54652
Updated by: rasmus@php.net
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:
Because you have $price=50 there instead of $price==50
Previous Comments:
------------------------------------------------------------------------
[2011-05-03 02:23:16] 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 01:16:43] 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-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