Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to check if variable is a "large integer"

Tags:

php

I need to check if a parameter (either string or int or float) is a "large" integer. By "large integer" I mean that it doesn't have decimal places and can exceed PHP_INT_MAX. It's used as msec timestamp, internally represented as float.

ctype_digit comes to mind but enforces string type. is_int as secondary check is limited to PHP_INT_MAX range and is_numeric will accept floats with decimal places which is what I don't want.

Is it safe to rely on something like this or is there a better method:

if (is_numeric($val) && $val == floor($val)) {
    return (double) $val;
}
else ...
like image 559
andig Avatar asked Dec 16 '13 09:12

andig


People also ask

How do you check if a variable is an integer in PHP?

The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.

Is Big int PHP?

PHP supports integer numbers with a maximum value of PHP_INT_MAX , a value defined by your processor architecture and available memory. If you need to manage integers bigger than PHP_INT_MAX , you need to use external libraries or PHP extensions such as GMP or BC Math.

What is %s and %D in PHP?

From the documentation: d - the argument is treated as an integer, and presented as a (signed) decimal number. s - the argument is treated as and presented as a string.

How do you check if a number is whole in PHP?

if y is anything other then a whole number the result is not a zero (0). A test then would be: if (y % 1 == 0) { // this is a whole number } else { // this is not a whole number } var isWhole = (y % 1 == 0? true: false); // to get a boolean return.


2 Answers

I recommend the binary calculator as it does not care about length and max bytes. It converts your "integer" to a binary string and does all calculations that way.

BC math lib is the only reliable way to do RSA key generation/encryption in PHP, and so it can easy handle your requirement:

$userNumber = '1233333333333333333333333333333333333333312412412412';

if (bccomp($userNumber, PHP_INT_MAX, 0) === 1) {
    // $userNumber is greater than INT MAX
}

Third parameter is the number of floating digits.

like image 180
Daniel W. Avatar answered Oct 06 '22 23:10

Daniel W.


So basically you want to check if a particular variable is integer-like?

function isInteger($var)
{
    if (is_int($var)) {
        // the most obvious test
        return true;
    } elseif (is_numeric($var)) {
        // cast to string first
        return ctype_digit((string)$var);
    }
    return false;
}

Note that using a floating point variable to keep large integers will lose precision and when big enough will turn into a fraction, e.g. 9.9999999999991E+36, which will obviously fail the above tests.

If the value exceeds INT_MAX on the given environment (32-bit or 64-bit), I would recommend using gmp instead and persist the numbers in a string format.

like image 29
Ja͢ck Avatar answered Oct 06 '22 23:10

Ja͢ck