note 107326 modified in function.is-numeric by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 21:26:24 +0000 |
| Subject: | note 107326 modified in function.is-numeric by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187888@lists.php.net to get a copy of this message | ||
If you want the numerical value of a string, this will return a float or int
value: <?php function get_numeric($val)
{ if (is_numeric($val)) { return $val + 0;
} return
0; } ?> Example: <?php get_numeric('3');
// int(3) get_numeric('1.2'); //
float(1.2) get_numeric('3.0'); //
float(3) ?>
--was--
If you want the numerical value of a string, this will return a float or int value:
function get_numeric($val) {
if (is_numeric($val)) {
return $val + 0;
}
return 0;
}
Example:
get_numeric('3'); // int(3)
get_numeric('1.2'); // float(1.2)
get_numeric('3.0'); // float(3)
http://php.net/manual/en/function.is-numeric.php