php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43839 Cast problem with the equal operator (==)
Submitted: 2008-01-14 13:33 UTC Modified: 2008-01-14 15:41 UTC
From: technique at ircf dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2CVS-2008-01-14 (snap) OS: Linux Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: technique at ircf dot fr
New email:
PHP Version: OS:

 

 [2008-01-14 13:33 UTC] technique at ircf dot fr
Description:
------------
I have noticed that the equal operator will systematically try to cast expressions as integers, even when it is useless.

This may cause problems like this (see the code below).

Wouldn't be better to cast only one operand ONLY if the two operands are not of the same type ? For example :

"1" == 1 , would cast the right operand (1) as a string and then compare it with the first

"123" == "456", would not cast any operand because they already have the same type

I know that this issue can be solved using the identity operator (===) but I find weird that "001" == "0001" returns true, or am I missing something ?

Reproduce code:
---------------
<?php
if ("0001" == "000001"){
	echo "Impossible !!!";
}
?>

Expected result:
----------------
Nothing

Actual result:
--------------
Impossible !!!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-14 15:41 UTC] colder@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Numeric strings are understood as numeric values. This is not exactly an int cast.

i.e. "01a" == "001a" is not true, as the operands are not numeric.
however (int)"01a" == (int)"001a" == 1
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 23:01:33 2025 UTC