note 119138 added to function.is-int
| From: | VasiliyMakogon | Date: | Fri, 08 Apr 2016 10:10:48 +0000 |
| Subject: | note 119138 added to function.is-int | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205184@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 84.234.75.250
Probable Submitter: 158.255.80.214
----
Manual Page -- http://php.net/manual/en/function.is-int.php
Edit -- https://master.php.net/note/edit/119138
Del: integrated -- https://master.php.net/note/delete/119138/integrated
Del: useless -- https://master.php.net/note/delete/119138/useless
Del: bad code -- https://master.php.net/note/delete/119138/bad+code
Del: spam -- https://master.php.net/note/delete/119138/spam
Del: non-english -- https://master.php.net/note/delete/119138/non-english
Del: in docs -- https://master.php.net/note/delete/119138/in+docs
Del: other reasons-- https://master.php.net/note/delete/119138
Reject -- https://master.php.net/note/reject/119138
Search -- https://master.php.net/manage/user-notes.php