Inspekt\Inspekt::isInt PHP Method

isInt() public static method

Returns true if value is a valid integer value, false otherwise.
public static isInt ( string | array $value ) : boolean
$value string | array
return boolean
    public static function isInt($value)
    {
        /**
         * we exit here if not is_numeric, because str_replace below will cast bools
         * to numerics
         */
        if (is_object($value) || is_bool($value)) {
            return false;
        }
        $locale = localeconv();
        // convert decimal sep
        $value = str_replace($locale['decimal_point'], '.', $value);
        // remove thousands sep
        $value = str_replace($locale['thousands_sep'], '', $value);
        // Must be an integer (no floats or e-powers))
        if (preg_replace("/^-?([0-9]+)\$/", "", $value) !== "") {
            return false;
        }
        // Must be greater than than min of 64-bit
        if (bccomp($value, "-9223372036854775807") < 0) {
            return false;
        }
        // Must be less than max of 64-bit)
        if (bccomp($value, "9223372036854775807") > 0) {
            return false;
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  *
  */
 public function testIsInt4()
 {
     $input = 2147483647;
     $this->assertTrue(Inspekt::isInt($input));
 }