-4

I was reading an article and I found this example:

// Prints bool(true)
var_dump('9223372036854775807' == '9223372036854775808');

// Prints bool(false)
var_dump('9223372036854775807' ==='9223372036854775808');

Anyone can explain me why?

Alessandro.Vegna
  • 1,262
  • 10
  • 19

4 Answers4

2

From reddit and a php bug report.

A few things are happeing here.

== doesn't return false if the values are of different type. In this case PHP is converting the strings to floats to compare the numbers and due to thier size and the way PHP handles floats they are rounded to the same number, hence comparing the 2 strings as numbers they are the same.

=== enforces type must be same. So comparing those two as strings they are not the same.

As mentioned by @Classified in another comment this was fixed in php 5.4.4 so isn't a current bug.

Arcath
  • 4,331
  • 9
  • 39
  • 71
1

The both cases should return FALSE because the numbers are different, but I think your doubt is about The difference between '==' and '===' AND why first case returns true right ?

When you use the '==' you're comparing if the values are equals BEFORE type-juggling, when you use the '===' you're comparing if the values equals AFTER type-juggling. In another words, when using '==' you're comparing if values are equals, but when you're using '===' you're comparing if values and types are equals.

In these cases, both comparisons should return FALSE because the values are different, but about the first comparison returning TRUE, it is a bug in PHP, as you can see in: https://bugs.php.net/bug.php?id=54547

PS: 9223372036854775807 is the bigger int number, so if you check types with gettype(), you will see:

9223372036854775807 is an integer.
9223372036854775808 is a double.

-2

As of PHP 5.4.4 this is no longer an issue.

The reason why this is happening, is because of type-juggling. In the first example you're checking if the two values are equal AFTER type-juggling so effectively it's comparing two string types.

And in the other example you are checking whether the two values are identical, as in the same type and the exact same value.

Check this.

Classified
  • 560
  • 1
  • 7
  • 21
-2
<?php
var_dump('9223372036854775807' == '9223372036854775808');
var_dump('9223372036854775807' ==='9223372036854775808');

Output:

bool(false) 
bool(false)

It doesn't. See your exact code above running here for proof https://3v4l.org/4oHvo

UPDATE:

Based on the below comments, it looks like the person asking is using a version of PHP that is 4 years old and no longer supported, and potentially vulnerable to security flaws. Update your PHP!

Please see the End of Life chart on php.net https://secure.php.net/eol.php

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • 1
    This only works on newer versions of PHP. After 5.4.4 this was the standard, but before then you would receive the result specified in the example. – Classified May 25 '18 at 10:38
  • 1
    @Classified I was just testing that, seeing as googling the numbers in the example gives you the php bug report. – Arcath May 25 '18 at 10:39
  • 1
    We do not support dead and potentially insecure PHP. Please see the End of Life charts, and recommend to anyone that they upgrade immediately https://secure.php.net/eol.php – delboy1978uk May 25 '18 at 10:40