php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81452 if comparison with '==' operator between integer and a string
Submitted: 2021-09-17 08:59 UTC Modified: 2021-09-17 09:09 UTC
From: remy dot dernat at umontpellier dot fr Assigned: nikic (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 7.4.23 OS: linux kernel 5.13.14-100.fc33
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: remy dot dernat at umontpellier dot fr
New email:
PHP Version: OS:

 

 [2021-09-17 08:59 UTC] remy dot dernat at umontpellier dot fr
Description:
------------
I have a generic function which is comparing two string.

The success string in my code is "1" (an integer actually). Sometimes, the comparing string is something like a date DD/MM/YY or MM/DD/YY. If the first two characters are 1 or 01 it will result in a succeeded condition.

I do not want to test if the compared string is a date or not, because, as I previously said, I may have many values, and I still want a Ok result if I compare int "1" to "01" or "1" string (so, I do not want to use "===").

Is there something special with "/" character, which creates such an issue ? Indeed, if I try to do a cast from this special string to a float it results in the "1" value anyway.

Test script:
---------------
$str1 = "01/2/21";
//$str1 = "01";
$str2 = 1;

if($str1 == $str2) {
	echo "Ok !!!\n";
} else { echo "Not equal\n"; }

//echo var_dump($str1);
//echo "\n";
//echo var_dump($str2);
//echo "\n";

Expected result:
----------------
Not equal


Actual result:
--------------
Ok !!!


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-17 09:07 UTC] nikic@php.net
Use PHP 8 ;) https://3v4l.org/lvvg9
 [2021-09-17 09:07 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-09-17 09:09 UTC] remy dot dernat at umontpellier dot fr
Perfect ! Thanks :))
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 04:01:29 2024 UTC