note 118080 added to function.is-numeric
| From: | lihatskiymatgmaildotcom | Date: | Wed, 30 Sep 2015 11:26:26 +0000 |
| Subject: | note 118080 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203881@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 193.19.243.212
Probable Submitter: 193.19.243.212 (proxied: 193.151.240.138)
----
Manual Page -- http://php.net/manual/en/function.is-numeric.php
Edit -- https://master.php.net/note/edit/118080
Del: integrated -- https://master.php.net/note/delete/118080/integrated
Del: useless -- https://master.php.net/note/delete/118080/useless
Del: bad code -- https://master.php.net/note/delete/118080/bad+code
Del: spam -- https://master.php.net/note/delete/118080/spam
Del: non-english -- https://master.php.net/note/delete/118080/non-english
Del: in docs -- https://master.php.net/note/delete/118080/in+docs
Del: other reasons-- https://master.php.net/note/delete/118080
Reject -- https://master.php.net/note/reject/118080
Search -- https://master.php.net/manage/user-notes.php