ID:              25566
 User updated by: Jared dot Williams1 at ntlworld dot com
 Reported By:     Jared dot Williams1 at ntlworld dot com
 Status:          Open
 Bug Type:        Scripting Engine problem
 PHP Version:     4.3.3
 New Comment:

Oh erm, seems its not limited to switch either. :/

echo('a' == 0 ? 'YES' : 'NO');
Outputs: YES

echo('3a' == 3 ? 'YES' : 'NO');
Outputs: YES

echo(4 == '4a' ? 'YES' : 'NO');
Outputs: YES

Surely these cant be considered equivalent.


Previous Comments:
------------------------------------------------------------------------

[2003-09-20 05:51:09] Jared dot Williams1 at ntlworld dot com

The non-numeric string values are getting cast to integer value 0,
which then match case 0:.

$t = array(10,1,2,3,'*','+','3a', 'b');
foreach($t as $v)
        switch ($v)
        {
                case 10: echo('0'); break;
                case 1: echo('1'); break;
                case 2: echo('2'); break;
                case 3: echo('3'); break;
                case '*': echo('*'); break;
                default: echo($v); break;
        }
Outputs:
0123*+3b

as 3a gets cast to 3. so seems there is a missing a condition in the
code somewhere, to see if the switched expression can actually be a
numeric type.


Also

$t = array(10,1,2,3,'*','+','3a', 'b');
foreach($t as $v)
        switch ($v)
        {
                case 10: echo('0'); break;
                case 1: echo('1'); break;
                case 2: echo('2'); break;
                case '*': echo('*'); break;
                case '3a': echo('!!'); break;
                default: echo($v); break;
        }
?>

Outputs:
012!!*+!!b

So the case labels are getting cast too without the check.

------------------------------------------------------------------------

[2003-09-20 00:40:40] shadowfaxx at cc dot com

To add to the above, it seems that the '0' case is the culprit. If you
use the code below where '10' replaces '0', the weird behavior does not
occur:

$t = array(10,1,2,3,'*','+','a', 'b');
foreach($t as $v)
        switch ($v)
        {
                case 10: echo('0'); break;
                case 1: echo('1'); break;
                case 2: echo('2'); break;
                case 3: echo('3'); break;
                case '*': echo('*'); break;
                default: echo($v); break;
        }
OutPut: 0123*+ab


So it seems that Case 0: grabs all the cases which are of a different
data type if the case expressions are not of the same type i.e. numeric
and string.

------------------------------------------------------------------------

[2003-09-16 18:47:44] Jared dot Williams1 at ntlworld dot com

"The first 5 array" should read "The first 4 array"

------------------------------------------------------------------------

[2003-09-16 18:41:54] Jared dot Williams1 at ntlworld dot com

Description:
------------
Switched expressions seem to be cast to a type before case matching
occurs.

If all case expressions are of the same type, either numeric, or
string, this problem does not arise.

It is not clear why, or really what type the switched expression is
cast to before comparision.





Reproduce code:
---------------
$t = array(0,1,2,3,'*','+','a', 'b')
foreach($t as $v)
        switch ($v)
        {
                case 0: echo('0'); break;
                case 1: echo('1'); break;
                case 2: echo('2'); break;
                case 3: echo('3'); break;
                case '*': echo('*'); break;
                default: echo($v); break;
        }

Expected result:
----------------
0123*+ab


The first 5 array elements are output their respective case statements,
and the latter 3 are handled by the default case.

Actual result:
--------------
01230000 

The first 5 array elements are output their respective case statements,
and the latter 3 are handled the 0 case.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=25566&edit=1

Reply via email to