Bug->Req #66211 [Opn->Nab]: integer/string comparisons have bad type-casting
| From: | yohgaki@php.net | Date: | Sun, 01 Dec 2013 02:58:24 +0000 |
| Subject: | Bug->Req #66211 [Opn->Nab]: integer/string comparisons have bad type-casting | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183037@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66211&edit=1
ID: 66211
Updated by: yohgaki@php.net
Reported by: php at charles-chandler dot org
Summary: integer/string comparisons have bad type-casting
-Status: Open
+Status: Not a bug
-Type: Bug
+Type: Feature/Change Request
Package: Variables related
Operating System: Mac OSX
PHP Version: 5.4.22
Block user comment: N
Private report: N
New Comment:
PHP converts "integer like string to integer" and PHP is designed to do so.
Use === or !== for stricter comparison.
Added to wiki
https://wiki.php.net/rfc/comparison_inconsistency?&#conversioncomparison
Previous Comments:
------------------------------------------------------------------------
[2013-12-01 02:14:51] php at charles-chandler dot org
Description:
------------
---
From manual page: http://www.php.net/types.comparisons
---
2 == '2b'
I understand why, in a loosely typed language, 2 == '2', but it doesn't make sense
for an integer to be equal-equal to a string that simply begins with an integer, such as 2 ==
'2b', so this looks like a bug to me. Rather than casting the string to an integer (i.e.,
intval('2b') == 2), the integer should cast to a string for the comparison (i.e.,
strval(2) != '2b').
Tested in PHP 5.2.5 and 5.3.13.
Test script:
---------------
<?php
// this is the problem, which we'd expect
// to return false, but which returns true:
echo (2 == '2b').'<br />';
// this is probably what's happening:
echo (2 == intval('2b')).'<br />';
// this is what probably should happen:
echo (strval(2) != '2b').'<br />';
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66211&edit=1