php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46208 Comparing integer to string loses leading 0's
Submitted: 2008-10-01 06:19 UTC Modified: 2008-10-02 09:51 UTC
From: brennan at reverseproductions dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: OSX
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: brennan at reverseproductions dot com
New email:
PHP Version: OS:

 

 [2008-10-01 06:19 UTC] brennan at reverseproductions dot com
Description:
------------
When comparing an integer with a string any leading 0's in the integers 
are ignored. 






Reproduce code:
---------------
$day = "05";
echo "05 == 05: ";
if (!(strcmp(05, $day)))
 echo "equal";
else
 echo "not equal";

 echo "<br>"; 
 
echo "07 == 05: "; 
if (!(strcmp(07, $day)))
 echo  "equal";
else
 echo "not equal";

Expected result:
----------------
05 == 05: equal
07 == 05: not equal

Actual result:
--------------
05 == 05: not equal
07 == 05: not equal

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-01 09:20 UTC] tularis@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

05 is an integer, not a string. And as far as integers go, 5 === 05 === 000000005. If you want it to be a string, declare it as a string: '05'.
 [2008-10-02 03:01 UTC] brennan at reverseproductions dot com
The issue is that the PHP language parser does seem to allow for 
comparing integers with strings.  As an example evaluate 5 == "5".  
Because of this, I would expect the integers, when automatically 
converted to a string, to be converted in a way which leaves the leading 
zeroes.  To reword: when PHP converts 05 to a string behind the scenes 
to evaluate 05 == "05" it should leave the leading 0's.

Maybe this ticket would be better marked as enhancement?
 [2008-10-02 09:51 UTC] scottmac@php.net
Integers prefixed with 0 are octal numbers, so 05 is really 5.

011 == 9

It's working exactly as designed.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 04:01:33 2025 UTC