Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 1234 == '1234 test' evaluate to true? [duplicate]

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')
like image 996
Cjueden Avatar asked Aug 28 '12 02:08

Cjueden


People also ask

What is difference == and === in php?

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: operand1 == operand2. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

How do you check if something is equal in PHP?

Equal Operator == The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same.


3 Answers

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.

like image 101
Lucas Green Avatar answered Oct 23 '22 21:10

Lucas Green


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

  • Comparison operators
  • Type juggling
like image 41
Lusitanian Avatar answered Oct 23 '22 20:10

Lusitanian


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
like image 28
Niet the Dark Absol Avatar answered Oct 23 '22 20:10

Niet the Dark Absol