3

Possible Duplicate:
comparing two variables returns false result

<?php
if( "32 is this a bug of php " == 32)
   echo "true";
else
   echo "false";
?>

output is :

true

you can see its output at http://codepad.org/hgOisqZ8

why this condition is evaluated as true?

Community
  • 1
  • 1
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

5 Answers5

7

because PHP is quite dumb when it comes to type conversion.

This expression is evaluated by first casting "32 is this a bug of php " to integer, which results with 32. Then comparison results in true.

If you want to make type-safe comparison use === operator

<?php
if( "32 is this a bug of php " === 32)
   echo "true";
else
   echo "false";
?>

Output will be false.

Note that with === operator we've got "32" !== 32 because one variable is string when other is int

Mariusz Sakowski
  • 3,232
  • 14
  • 21
6

This is discussed in the PHP Manual.

String conversion to numbers

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

The string will be evaluated as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an integer.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

Note the part that states

If the string starts with valid numeric data, this will be the value used.

Since your string starts with 32 PHP will compare if(32 == 32) which will be true.

Use type safe checks, that takes the datatype into consideration, when dealing with types that could be different if this behaviour is not desired. Like

1 === 1:   true
1 == 1:    true
1 === "1": false
1 == "1":  true
"foo" === "foo": true
inquam
  • 12,664
  • 15
  • 61
  • 101
5

See the documentation for comparison operators:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

So no, it isn't a bug, it is how the language is designed.

Use === if you don't want type juggling.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

For attention, also you need to know $a==$b && $b==$c doesn't mean $a == $c in php.

Example:

var_dump('32E0' == '32');
var_dump('32' == 32);
var_dump(32 == '32 is a bug');
var_dump('32E0' == '32 is a bug'); 

Output is:

bool(true)
bool(true)
bool(true)
bool(false)
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • This is since no conversion has to be done when comparing the two strings. So the strings can be handled as strings and not numerical values. – inquam Oct 15 '12 at 09:49
  • @inquam "no conversion has to be done when comparing two strings" is **not right** in php, see http://stackoverflow.com/questions/12598407/php-expresses-two-different-strings-to-be-the-same – xdazz Oct 15 '12 at 09:50
  • For this specific case I meant. But true indeed. PHP can play tricks on our minds ;) – inquam Oct 15 '12 at 09:52
1

you can also use like this

<?php
if( "32 is this a bug of php " == '32')
  echo "true";
else
  echo "false";
?>
M.I.T.
  • 1,040
  • 2
  • 17
  • 34