note 83118 added to function.is-int
| From: | JulienPicalausa at osu1 dot php dot net | Date: | Sun, 11 May 2008 09:44:12 +0000 |
| Subject: | note 83118 added to function.is-int | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139265@lists.php.net to get a copy of this message | ||
To check if a string ($s) is a representation of an integer (including representations is scientific
notation and negative numbers), you can use the following test, provided that you don't expect
values that are out of bounds for an integer.
is_numeric($s) && floatval($s) == intval(floatval($s))
If the test returns true, the string is a representation of an integer.
is_numeric (if it works as intended) protects from strings that are not proper numbers.
The comparison filters anything that is non_integer
If, for performance reasons, you want to avoid converting to float twice, it can also be written:
is_numeric($s) && ($f = floatval($s)) == intval($f)
If you plan to get values that cannot be representated with an integer and are ready to deal with a
float, you can use floor instead of intval, as long as you are ready to deal with floats. Even so,
that method will become unreliable when the precision of the float becomes insufficient for getting
to the fractional part of the number
----
Server IP: 128.39.198.38
Probable Submitter: 80.203.37.140
----
Manual Page -- http://www.php.net/manual/en/function.is-int.php
Edit -- https://master.php.net/note/edit/83118
Del: integrated -- https://master.php.net/note/delete/83118/integrated
Del: useless -- https://master.php.net/note/delete/83118/useless
Del: bad code -- https://master.php.net/note/delete/83118/bad+code
Del: spam -- https://master.php.net/note/delete/83118/spam
Del: non-english -- https://master.php.net/note/delete/83118/non-english
Del: in docs -- https://master.php.net/note/delete/83118/in+docs
Del: other reasons-- https://master.php.net/note/delete/83118
Reject -- https://master.php.net/note/reject/83118
Search -- https://master.php.net/manage/user-notes.php