note 119140 deleted from function.is-numeric by crell

From: Date: Mon, 18 Apr 2022 14:59:18 +0000
Subject: note 119140 deleted from function.is-numeric by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218287@lists.php.net to get a copy of this message
Note Submitter: Vasiliy Makogon ---- If you want detect integer of float values, which presents as pure int or float, and presents as string values, use this functions: <?php function isInteger($val) { if (!is_scalar($val) || is_bool($val)) { return false; } if (is_float($val + 0) && ($val + 0) > PHP_INT_MAX) { return false; } return is_float($val) ? false : preg_match('~^((?:\+|-)?[0-9]+)$~', $val); } function isFloat($val) { if (!is_scalar($val)) { return false; } return is_float($val + 0); } foreach ([ '11111111111111111', 11111111111111111, // > PHP_INT_MAX - presents in PHP as float 1, '10', '+1', '1.1', 1.1, .2, 2., '.2', '2.', '-2.', '-.2', null, [], true, false, 'string' ] as $value) { echo $value . ':' . gettype($value) . ' is Integer? - ' . (isInteger($value) ? 'yes' : 'no') . PHP_EOL; echo $value . ':' . gettype($value) . ' is Float? - ' . (isFloat($value) ? 'yes' : 'no') . PHP_EOL; } ?> Output: 11111111111111111:string is Integer? - no 11111111111111111:string is Float? - yes 1,1111111111111E+16:double is Integer? - no 1,1111111111111E+16:double is Float? - yes 1:integer is Integer? - yes 1:integer is Float? - no 10:string is Integer? - yes 10:string is Float? - no +1:string is Integer? - yes +1:string is Float? - no 1.1:string is Integer? - no 1.1:string is Float? - yes 1,1:double is Integer? - no 1,1:double is Float? - yes 0,2:double is Integer? - no 0,2:double is Float? - yes 2:double is Integer? - no 2:double is Float? - yes .2:string is Integer? - no .2:string is Float? - yes 2.:string is Integer? - no 2.:string is Float? - yes -2.:string is Integer? - no -2.:string is Float? - yes -.2:string is Integer? - no -.2:string is Float? - yes :NULL is Integer? - no :NULL is Float? - no Array:array is Integer? - no Array:array is Float? - no 1:boolean is Integer? - no 1:boolean is Float? - no :boolean is Integer? - no :boolean is Float? - no string:string is Integer? - no string:string is Float? - no

« previous php.notes (#218287) next »