How to trigger integer overflow in PHP [duplicate] - php

Ignoring the special libraries that allow you to work with very big numbers, what's the largest int value you can store in PHP?

From the PHP manual:
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).
PHP does not support unsigned
integers. Integer size can be
determined using the constant
PHP_INT_SIZE, and maximum value using
the constant PHP_INT_MAX since PHP
4.4.0 and PHP 5.0.5.
64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit.

32-bit builds of PHP:
Integers can be from -2,147,483,648 to 2,147,483,647 (~ ± 2 billion)
64-bit builds of PHP:
Integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (~ ± 9 quintillion)
Numbers are inclusive.
Note: some 64-bit builds once used 32-bit integers, particularly older Windows builds of PHP
Values outside of these ranges are represented by floating point values, as are non-integer values within these ranges. The interpreter will automatically determine when this switch to floating point needs to happen based on whether the result value of a calculation can't be represented as an integer.
PHP has no support for "unsigned" integers as such, limiting the maximum value of all integers to the range of a "signed" integer.

The size of PHP ints is platform dependent:
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).
PHP does not support unsigned
integers. Integer size can be
determined using the constant
PHP_INT_SIZE, and maximum value using
the constant PHP_INT_MAX since PHP
4.4.0 and PHP 5.0.5.
PHP 6 adds "longs" (64 bit ints).

(a little bit late, but could be useful)
Only trust PHP_INT_MAX and PHP_INT_SIZE, this value vary on your arch (32/64 bits) and your OS...
Any other "guess" or "hint" can be false.

Ah I found it: 232 - 1 (2147483647)
http://au2.php.net/int
Integer overflow
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
<?php
$large_number = 2147483647;
var_dump($large_number);
// output: int(2147483647)
$large_number = 2147483648;
var_dump($large_number);
// output: float(2147483648)

It depends on your OS, but 2147483647 is the usual value, according to the manual.

It subjects to architecture of the server on which PHP runs. For 64-bit,
print PHP_INT_MIN . ", ” . PHP_INT_MAX; yields -9223372036854775808, 9223372036854775807

Although PHP_INT_* constants exist for a very long time, the same MIN / MAX values could be found programmatically by left shifting until reaching the negative number:
$x = 1;
while ($x > 0 && $x <<= 1);
echo "MIN: ", $x;
echo PHP_EOL;
echo "MAX: ", ~$x;

Related

why does this (int) cast not work with DateTime::format as expected? [duplicate]

Ignoring the special libraries that allow you to work with very big numbers, what's the largest int value you can store in PHP?
From the PHP manual:
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).
PHP does not support unsigned
integers. Integer size can be
determined using the constant
PHP_INT_SIZE, and maximum value using
the constant PHP_INT_MAX since PHP
4.4.0 and PHP 5.0.5.
64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit.
32-bit builds of PHP:
Integers can be from -2,147,483,648 to 2,147,483,647 (~ ± 2 billion)
64-bit builds of PHP:
Integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (~ ± 9 quintillion)
Numbers are inclusive.
Note: some 64-bit builds once used 32-bit integers, particularly older Windows builds of PHP
Values outside of these ranges are represented by floating point values, as are non-integer values within these ranges. The interpreter will automatically determine when this switch to floating point needs to happen based on whether the result value of a calculation can't be represented as an integer.
PHP has no support for "unsigned" integers as such, limiting the maximum value of all integers to the range of a "signed" integer.
The size of PHP ints is platform dependent:
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).
PHP does not support unsigned
integers. Integer size can be
determined using the constant
PHP_INT_SIZE, and maximum value using
the constant PHP_INT_MAX since PHP
4.4.0 and PHP 5.0.5.
PHP 6 adds "longs" (64 bit ints).
(a little bit late, but could be useful)
Only trust PHP_INT_MAX and PHP_INT_SIZE, this value vary on your arch (32/64 bits) and your OS...
Any other "guess" or "hint" can be false.
Ah I found it: 232 - 1 (2147483647)
http://au2.php.net/int
Integer overflow
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
<?php
$large_number = 2147483647;
var_dump($large_number);
// output: int(2147483647)
$large_number = 2147483648;
var_dump($large_number);
// output: float(2147483648)
It depends on your OS, but 2147483647 is the usual value, according to the manual.
It subjects to architecture of the server on which PHP runs. For 64-bit,
print PHP_INT_MIN . ", ” . PHP_INT_MAX; yields -9223372036854775808, 9223372036854775807
Although PHP_INT_* constants exist for a very long time, the same MIN / MAX values could be found programmatically by left shifting until reaching the negative number:
$x = 1;
while ($x > 0 && $x <<= 1);
echo "MIN: ", $x;
echo PHP_EOL;
echo "MAX: ", ~$x;

Having an issue converting a float to an integer (PHP)

Short story, here's my code:
var_dump($recentGame->createDate);
var_dump((int) $recentGame->createDate);
Is producing the following output:
float 1416772859827
int -566347853
Why is the output of the typecasted output not this:
int 1416772859827
Quoting from the PHP docs:
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 for Windows, which is always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

JavaScript VS PHP: The same calculation returns different result

I'm trying to do some simple math (?) in JavaScript:
( 1023 * 2 ) + 76561197960265728;
I already did the same calculation in PHP, but the results are different:
JavaScript: 76561197960267780
PHP: 76561197960267774 (the right result)
I then tried the following:
http://jsfiddle.net/YxBa4/
Is there a "limit" in JavaScript for high numbers in calculations?
//edit:
Thanks for your answers, I now use the BigNumber one.
My full working code now:
on JSFiddle
In Javascript, numbers are 64 bit floating point values. The largest integer (magnitude) is 253, or Math.pow(2,53), or 9007199254740992. taken from: http://notepad2.blogspot.ca/2012/04/maximum-integer-in-javascript.html
You are 1351079458714420 over the limit.
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. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5. taken from http://php.net/manual/en/language.types.integer.php
So basically PHP allows you more capacity for Integer values according to the PHP configuration.
The value 76561197960265730 id greater than the maximum allowed number size in javascript. Note that there are no real integers in Javascript just the Number type which is always a 64bit floating point value and platform independent. But the largest possible integer value is just 2^53 because 11 bits are at least reserved for the numbers after the comma and the signage bit.
In your example you can see this by simply doing:
alert(76561197960265728);
What will give you already an error, even without any calculations. (output: '76561197960265730')
In php the maximum integer value depends on your system. If you are on a 64 bit system, the MAX integer value is 2^64 what is greater than (2 * 1023) + 76561197960265728. That'swhy the calculation succeeded in PHP - on a 64 bit system
In PHP you can detect the maximum integer size on your system by reading the constant PHP_INT_MAX and `
You could use the bcmath extension for PHP and the JS conversion of it to get consistant results across the 2 languages:
bcadd('76561197960267774', '76561197960267774');
// 153122395920535548
http://jsfiddle.net/zELmm/
http://phpjs.org/functions/bcadd/
http://php.net/manual/en/function.bcadd.php
Update:-
BigInt was added as a native features of JavaScript.
But still there are some precision error on round off, A workaround Comparison
For example here full code for comparing BigInt with Number in JavaScript
As per Initial Problem Here :-
PHP: 76561197960267774 (the right result)
var num = BigInt( 1023 * 2 ) + 76561197960265728n;
console.log(num.toString());
/*
num.toString() removes n at the end of digit.
Output: 76561197960267774
*/
Where bigint, created by appending n to the end of an integer literal or by calling the BigInt() constructor.
So after using BigInt:-
JavaScript: 76561197960267774 (the right result)

Confusion in int conversion of a string in PHP

I am unable to get the exact value of $data, which I converted into integer from this,
<?php
$data = '9999999999';
echo $datan = (int) $data;
?>
How can I get the exact value of $data from $datan?
From the PHP docs:
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. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
http://php.net/manual/en/language.types.integer.php
Check the PHP_INT_MAX of your system.
If you get 2147483647, it means PHP_INT_MAX is 2147483647 in your system(32 bits signed).
Check you PHP_INT_MAX. it may be lower than 9999999999

What's the maximum value for an int in PHP?

Ignoring the special libraries that allow you to work with very big numbers, what's the largest int value you can store in PHP?
From the PHP manual:
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).
PHP does not support unsigned
integers. Integer size can be
determined using the constant
PHP_INT_SIZE, and maximum value using
the constant PHP_INT_MAX since PHP
4.4.0 and PHP 5.0.5.
64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit.
32-bit builds of PHP:
Integers can be from -2,147,483,648 to 2,147,483,647 (~ ± 2 billion)
64-bit builds of PHP:
Integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (~ ± 9 quintillion)
Numbers are inclusive.
Note: some 64-bit builds once used 32-bit integers, particularly older Windows builds of PHP
Values outside of these ranges are represented by floating point values, as are non-integer values within these ranges. The interpreter will automatically determine when this switch to floating point needs to happen based on whether the result value of a calculation can't be represented as an integer.
PHP has no support for "unsigned" integers as such, limiting the maximum value of all integers to the range of a "signed" integer.
The size of PHP ints is platform dependent:
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).
PHP does not support unsigned
integers. Integer size can be
determined using the constant
PHP_INT_SIZE, and maximum value using
the constant PHP_INT_MAX since PHP
4.4.0 and PHP 5.0.5.
PHP 6 adds "longs" (64 bit ints).
(a little bit late, but could be useful)
Only trust PHP_INT_MAX and PHP_INT_SIZE, this value vary on your arch (32/64 bits) and your OS...
Any other "guess" or "hint" can be false.
Ah I found it: 232 - 1 (2147483647)
http://au2.php.net/int
Integer overflow
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
<?php
$large_number = 2147483647;
var_dump($large_number);
// output: int(2147483647)
$large_number = 2147483648;
var_dump($large_number);
// output: float(2147483648)
It depends on your OS, but 2147483647 is the usual value, according to the manual.
It subjects to architecture of the server on which PHP runs. For 64-bit,
print PHP_INT_MIN . ", ” . PHP_INT_MAX; yields -9223372036854775808, 9223372036854775807
Although PHP_INT_* constants exist for a very long time, the same MIN / MAX values could be found programmatically by left shifting until reaching the negative number:
$x = 1;
while ($x > 0 && $x <<= 1);
echo "MIN: ", $x;
echo PHP_EOL;
echo "MAX: ", ~$x;

Categories