80

Possible Duplicate:
php == vs === operator

An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true?

(1234 == '1234 test')
24
  • 212
    Because that's how PHP rolls: Like a square wheel. Aug 28, 2012 at 2:26
  • 12
    @alfasin: Yes I can. The comparison in the question is completely nonsensical, and there's no reason it shouldn't be considered an error, even if for the programmer's sake. Aug 28, 2012 at 2:31
  • 20
    @Lusitanian: "... there are likely some legitimate use cases for this sort of comparison." Find one, I dare you. Aug 28, 2012 at 2:43
  • 7
    @alfasin But it's not 1234 == '1234' in question (which is somewhat understandable without a complex reason); here the question is about 1234 == '1234 test' (which would be false in JavaScript, and is much less "expected" at first glance).
    – user166390
    Aug 28, 2012 at 3:33
  • 8
    Right, it's always the programmers fault when they shoot themselves in the foot, even when the language has 6 triggers, and both barrel aim downwards Aug 28, 2012 at 7:40

6 Answers 6

96

Because you are using the == (similarity) operator and PHP is coercing the string to an int.

To resolve it use the === (equality) operator, which checks not only if the value is the same, but also if the data type is the same, so "123" string and 123 int won't be considered equal.

2
78

In PHP (and JavaScript -- which has slightly different behavior), the comparison operator == works differently than it does in strongly-typed languages like C or Java. The === operator has the behavior that you most likely expect. Below is a breakdown of the two comparison operators as they apply to PHP.

==

This operator is officially known as the "equality" operator, though that doesn't really fit the normal definition of the word "equality". It does what is known as a type-juggling comparison. If the types of both operands don't match (in your example, 1234 was an integer and 1234 test was a string), PHP will implicitly cast the operands to each others' types and test the equality of the newly-typed values as shown below:

<?php
var_dump( (int) 'hi' ); // int(0)
var_dump( (string) 0 ); //string("0")
var_dump( 'hi' ==  0 ); // bool(true)

var_dump( (int) '1hi' ); // int(1)
var_dump( 1 == '1hi' ); // bool(true)

It has a counterpart (type-juggling) inequality operator, !=.

===

The === operator, known as the "identical" operator, performs a strict check of the value and type of both operands and does not perform any implicit casts. Therefore, "0" does not === 0 and "1234 test"does not === 1234.

<?php
var_dump( '1234 test' === 1234 ); // bool(false)

It has a counterpart (strict) inequality operator, !==.

Quirks

Note that the === operator has behavior on objects that is considered strange by some. Say we have class A and variables $a and $b as defined below:

<?php
class A { 
  public $property = 'default value';
}
$a = new A();
$b = new A();

You might expect var_dump($a === $b); to output bool(true). It will actually return false. When used upon objects, the operator actually checks if both operands are references to the same object. The == operator, in this instance, works by checking the properties of the objects, so $a == $b.

PHP Manual Links

5
  • 5
    Watch out: var_dump(0 == 'abc'); gives true but var_dump(0 == '1abc'); evaluates to false
    – rabudde
    Aug 28, 2012 at 6:31
  • Note that C also does the type juggling, the only difference is that it has a smaller set of implicit casts. Comparing an int against a float will result in loss of precision, and comparing an unsigned against an int will give unexpected results if the int is negative. Aug 28, 2012 at 6:35
  • 3
    Note that this is not the same behavior in JavaScript. 123 == "123 asd" returns false whereas 123 == "0123" return true.
    – Ekin Koc
    Aug 28, 2012 at 9:50
  • @rabudde because (int) '1abc' === 1. It's a similar case to 1234 == '1234 test'
    – Lusitanian
    Aug 28, 2012 at 11:37
  • @Lusitanian you're right. I want to give only a hint, what can happen when you don't pay attention and write your code quickly. Your example result is the same at the end, sure, but you force(!) the integer conversion. And I want to show the little special meaning of loose comparision to 0.
    – rabudde
    Aug 28, 2012 at 12:26
28

When casting a string to an integer, any numeric characters up to the first non-numeric character becomes the number. Thus '1234 test' becomes 1234 because space is not a numeric character.

Thus 1234 == '1234 test'

If you want to force a string comparison, you should cast to string:

''.(1234) == '1234 test' // implicit
(string) 1234 == '1234 test' // explicit
strval(1234) == '1234 test' // procedural
0
8

You are loosely comparing two different types of data (an integer and a string). PHP has a very detailed chart of how comparisons work in their system when using the loose comparison binary operator (==):

http://php.net/manual/en/types.comparisons.php

If you want to ensure that the types are also in sync, that is that they are both integers or both strings, use the strong type comparison operator (===).

Note that, when using this operator, this will also return false:

1234 === '1234'

If you are unsure of your types when comparing, you can couple the strong-type comparison with PHP typecasting:

$a = 1234;
$b = '1234';

if ($a === $b) { }            // Will not fire, as it is false
if ((int)$a === (int)$b) { }  // Will fire, as it is true
4

The double equals will tell php to parse an int from the string. The string will evaluate to the integer 1234. Use triple equals '===' to get exact comparison.

3

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

var_dump(0 == "a"); // 0 == 0 -> true

Not the answer you're looking for? Browse other questions tagged or ask your own question.