Bug #12876 [Com]: Comparison with zero (0) returns TRUE

From: Date: Thu, 09 Jun 2011 08:17:57 +0000
Subject: Bug #12876 [Com]: Comparison with zero (0) returns TRUE
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161258@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=12876&edit=1

 ID:                 12876
 Comment by:         pablo dot orensanz at gmail dot com
 Reported by:        ash at freaky-namuh dot com
 Summary:            Comparison with zero (0) returns TRUE
 Status:             Bogus
 Type:               Bug
 Package:            Strings related
 Operating System:   Linux Debian 2.2r2 (testing)
 PHP Version:        4.0.6
 Block user comment: N
 Private report:     N

 New Comment:

Hi!
I solved it by always comparing text strings. What do you think?

'"'.$value1.'"' === '"'.$value2.'"'


Previous Comments:
------------------------------------------------------------------------
[2001-08-21 10:57:34] 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.

------------------------------------------------------------------------
[2001-08-21 10:43:08] 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:31:29] 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:27:54] 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?


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=12876&edit=1


Thread (1 message)

  • pablo dot orensanz at gmail dot com
  • Unknown Message
    • pablo dot orensanz at gmail dot com
« previous php.bugs (#161258) next »