Comparions fails with big number PHP

php comparison operators
php ternary operator
php elvis operator
php vs performance
php 7 ?? operator
php compare integers
php string comparison
@ symbol in php

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?

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.

PHP :: Bug #54547 :: wrong equality of string numbers, I'm just gonna paste in that PHP Sadness article to show why this is such a big issue. According to php language.operators.comparison, the  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. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

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:

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.

Comparison Operators - Manual, If you compare a number with a string or the comparison involves numerical For example, comparing passwords with == may result in a very large security hole. to say, if PHP picked up any more ternary operators, this will be a problem. php-bignumbers . A robust library to handle immutable big numbers inside PHP applications The current stable version is 0.8.3. Litipk\BigNumbers supports PHP 7.x.

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.

Numerical strings comparison, First, when comparing strings, and a string looks like a number, it is converted to a we cannot compare these values right away (to tell which is bigger): given  PHP type comparison tables. The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes.

var_dump('9223372036854775807' == '9223372036854775808');
var_dump('9223372036854775807' ==='9223372036854775808');



It doesn't. See your exact code above running here for proof


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

Strict vs. Loose Comparisons in PHP, What's happening is that if a string starts with a number, PHP will pull it out and use it as the value for In most cases, loose comparisons aren't a problem. Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision.

PHP tip: 4 cases where you must use strict comparison, This is why you can, for example, directly compare numeric strings against <?​php $string = "It's bigger on the inside"; $substring = 'It'; $strpos = strpos($string, $​substring); if (! <br>'; } /* Correct */ if (sleep(1) === FALSE) { echo 'Sleep failed! Still have on mind, that if you convert big numbers by adding zero, PHP makes automatic "to a float" conversion, so it is same as floatVal(). So if the number is realy big (over 13 digits), you can lose preciosity. Do not use it for such long numbers, if all bits do matter (IPv6 addresses and similar).

Why is PHP's method of comparing different types bad?, The biggest problem is that an equivalence relationship, the mathy term for PHP tries to handle comparisons dynamically, but includes '===' to allow strict as strings, for example a GET request: The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers.

Arbitrary Precision and Big Numbers in PHP, Discover big numbers and arbitrary precision in PHP. To get the GMP PHP module, simply issue the following command in your terminal on and calculate the difference – I can tell you now that the difference is obvious. In this article, we will review the PHP capability to provide arbitrary precision number calculation / big integer calculation by reviewing 3 PHP modules: GMP, BC Math and php-bignumbers. We will

  • If it was in an article I assume you could have read a few lines more and got the answer.
  • The second comparison compares 2 strings, not 2 big numbers.
  • php version????
  • both of them returns bool(false) in php version 7.2.4, 5.6.20, 5.5.5
  • Can't replicate,
  • 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 I was just testing that, seeing as googling the numbers in the example gives you the php bug report.
  • We do not support dead and potentially insecure PHP. Please see the End of Life charts, and recommend to anyone that they upgrade immediately