#25566 [Bgs]: Casting of strings to integers is flawed, no checking for non numeric chars.
| From: | rasmus@php.net | Date: | Sun, 21 Sep 2003 15:59:18 +0000 |
| Subject: | #25566 [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-47904@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: Bogus
Bug Type: Scripting Engine problem
PHP Version: 4.3.3
New Comment:
No, but if you want them to be compared as strings, just cast to a
string then, or use a strcmp() call. PHP can't read your mind.
Previous Comments:
------------------------------------------------------------------------
[2003-09-21 11:51:32] Jared dot Williams1 at ntlworld dot com
I do understand that time has fixed this "feature".
As for explicit casting, it does result in different behaviour, when
comparing string/values.
$x = '0xF';
$y = (int)$x;
echo($x == 15 ? 'YES' : 'NO');
echo('<br />');
echo($y == 15 ? 'YES' : 'NO');
Outputs:
YES
NO
Strict equivalence testing doesnt help when you wish
"1234" and 1234 to be considered equal.
------------------------------------------------------------------------
[2003-09-20 20:17:33] rasmus@php.net
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.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
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