php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13304 string comparisons not working as expected
Submitted: 2001-09-14 12:50 UTC Modified: 2001-09-14 13:09 UTC
From: lee at mediawaveonline dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 4.0.6 OS: linux
Private report: No CVE-ID: None
 [2001-09-14 12:50 UTC] lee at mediawaveonline dot com
<?php
 function check($var_1, $var_2)
 {
  if ( $var_1 == $var_2 )
   echo "a true <br>\n";
  else
   echo "a false <br>\n";
  if ( (string)$var_1 == (string)$var_2 )
   echo "b true <br>\n";
  else
   echo "b false <br>\n";
  if ( strcmp($var_1, $var_2) == 0 )
   echo "c true <br>\n";
  else
   echo "c false <br>\n";
 }

 check('997310325024327300', '997310325024327300');
 check('997310325024327300', '997310325024327301');
?>

php-4.0.6

09:51 -root- /home/httpd/vhosts/blazers
php -q test.php 
a true <br>
b true <br>
c true <br>
a true <br>
b true <br>
c false <br>
09:51 -root- /home/httpd/vhosts/blazers

ok I can understand the first if() failing, php is conffused and things I want to compare int not strings, this is a peave because php only supports upto signed long int, which this is much larger, I think php should realize this and compare them as strings.

the second fails too .. ? this is strange because I have spec. type casted that as strings, they should compare fine as strings. this is where I beleive there is more then a quirk and more of a bug.

the third susceeds just as it should, its just not near as elegant as a simple if ($var_1 == $var_2).

-- 

  Chris Lee
  lee@mediawaveonline.com


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-09-14 13:09 UTC] hholzgra@php.net
(string) casting won't help as it takes place *before* autoconversion

just use strcmp() or the tripple === operator 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon May 05 22:01:30 2025 UTC