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')
== 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.
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.
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.
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, !==
.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With