note 118080 deleted from function.is-numeric by crell
| From: | crell@php.net | Date: | Mon, 18 Apr 2022 14:55:55 +0000 |
| Subject: | note 118080 deleted from function.is-numeric by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218275@lists.php.net to get a copy of this message | ||
Note Submitter: lihatskiym at gmail dot com
----
Check that value is whole numeric or is whole integer.
<?php
function whole_numeric($val)
{
if (is_numeric($val)
&& floor($val) == $val)
{
if ((string)$val === (string)0)
return true;
elseif(ltrim((string)$val, '0') === (string)$val)
return true;
}
return false;
}
function whole_int($val)
{
$val = strval($val);
$val = str_replace('-', '', $val);
if (ctype_digit($val))
{
if ($val === (string)0)
return true;
elseif(ltrim($val, '0') === $val)
return true;
}
return false;
}
?>
Result will be like this:
<?php
whole_numeric('-1.0'); // true
whole_numeric(0); // true
whole_numeric('0'); // true
whole_numeric('00'); // false
whole_numeric(1); // true
whole_numeric('1'); // true
whole_numeric('1.0'); // true
whole_numeric('1,0'); // false
whole_numeric('1.2'); // false
whole_numeric('001'); // false
whole_numeric(array()); // false
whole_int('-1.0'); // false
whole_int('-1'); // true
whole_int(0); // true
whole_int('0'); // true
whole_int('00'); // false
whole_int(1); // true
whole_int('1'); // true
whole_int('1.0'); // false
whole_int('1.2'); // false
whole_int('001'); // false
whole_int(array()); // false
?>