php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12876 Comparison with zero (0) returns TRUE
Submitted: 2001-08-21 10:27 UTC Modified: 2001-08-21 10:57 UTC
From: ash at freaky-namuh dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.0.6 OS: Linux Debian 2.2r2 (testing)
Private report: No CVE-ID: None
 [2001-08-21 10:27 UTC] ash at freaky-namuh dot com
When strings are compared to 0, they always return TRUE.

<?php
$test = 'somestring';
if( $test == 'somestring')
echo "Match 1\n";
if( $test == 0 )
echo "Match 2\n";
if( $test == 1 )
echo "Match 3\n";
if( $test != 0 )
echo "Match 4\n";
if( $test != 'somestring' )
echo "Match 5\n";
if( $test != 1 )
echo "Match 6\n";
?>

Expected Output:
Match 1


Actual Ouput:
Match 1
Match 2
Match 6

Is there something I'm missing?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-21 10:31 UTC] derick@php.net
You miss something :) Strings will be converted to a number first, which is not possible. The result of this conversion is 0, and (0 == 0) => TRUE.

Derick
 [2001-08-21 10:43 UTC] ash at freaky-namuh dot com
This never used to be the case.. < 4.0.4
So basically don't use == to compare strings?  Does PHP convert to numbers when doing 'test' == 'test'?

I have this situation

<?php
// snip

$row = pg_fetch_row($result, 0);

while( list($field, $value) == each($row) )
{
   if( $field == 'stateid' )
     echo get_state_selector($conn, $value);
   else
     echo $value;

}?>

pg_fetch_row by default returns keys as field names and as array elements.  get_state_selector gets outputed for both $row[0] and $row[stateid].  I know I can change pg_fetch_row (which I will) to output field names only, but I haven't run into this in the past.  Has something changed?  (There was a point pre 4.0.3 where zero (0) didn't equal empty, and now it does.. as in 
$test = 0; 
if( empty($test) ) 
echo "blah"; 
)




 [2001-08-21 10:57 UTC] ash at freaky-namuh dot com
The above should have read pg_fetch_array not pg_fetch_row.

pg_fetch_array without setting result_type to PGSQL_ASSOC returns both field numbers and field names as array indexes.
 [2011-06-09 10:17 UTC] pablo dot orensanz at gmail dot com
Hi!
I solved it by always comparing text strings. What do you think?

'"'.$value1.'"' === '"'.$value2.'"'
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC