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

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

Posted by: admin November 11, 2017 Leave a comment

Questions:

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')
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.

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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