#25566 [Opn->Bgs]: Casting of strings to integers is flawed, no checking for non numeric chars.
| From: | rasmus@php.net | Date: | Sun, 21 Sep 2003 00:17:34 +0000 |
| Subject: | #25566 [Opn->Bgs]: Casting of strings to integers is flawed, no checking for non numeric chars. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-47886@lists.php.net to get a copy of this message | ||
ID: 25566
Updated by: rasmus@php.net
Reported By: Jared dot Williams1 at ntlworld dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
PHP Version: 4.3.3
New Comment:
That's why you have the === operator. "12345aaaa" has always evaluated
to 12345 when cast to an integer and it always will. That also means
that it must be equivalent to 12345 when used in an integer comparison.
You need to be explicit in your comparisons and use explicit casting
elsewhere.
Previous Comments:
------------------------------------------------------------------------
[2003-09-20 19:29:23] Jared dot Williams1 at ntlworld dot com
The documentation does not help.
a string containing a beginning with a numeric character, but also
containing non-numerics surely should not be cast to an integer before
comparision?
I understand that "12345" == 12345 but the string "12345xxx" isnt
12345.
------------------------------------------------------------------------
[2003-09-20 17:58:26] sniper@php.net
See these manual pages:
http://www.php.net/switch
http://www.php.net/manual/en/types.comparisons.php
Maybe the manual should explain this a bit better.
------------------------------------------------------------------------
[2003-09-20 06:20:46] Jared dot Williams1 at ntlworld dot com
How's this for an impossibility..
echo('a' != 0 ? 'TRUE' : 'FALSE');
echo('<br />');
echo('a' ? 'TRUE' : 'FALSE');
Outputs:
FALSE
TRUE
------------------------------------------------------------------------
[2003-09-20 06:08:53] Jared dot Williams1 at ntlworld dot com
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.
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/25566
--
Edit this bug report at http://bugs.php.net/?id=25566&edit=1