note 95658 added to function.is-int
| From: | thierryreeuwijk at hotmail dot com | Date: | Thu, 14 Jan 2010 10:04:26 +0000 |
| Subject: | note 95658 added to function.is-int | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164962@lists.php.net to get a copy of this message | ||
If you only want integer values like 23 or 0155, or form/string integer values like "23"
or "0155" to be valid, this should work just fine.
<?php
function int($int){
// First check if it's a numeric value as either a string or number
if(is_numeric($int) === TRUE){
// It's a number, but it has to be an integer
if((int)$int == $int){
return TRUE;
// It's a number, but not an integer, so we fail
}else{
return FALSE;
}
// Not a number
}else{
return FALSE;
}
}
print("155".int(155)."<br>");
print("15.5".int(15.5)."<br>");
print("\"155\"".int("155")."<br>");
print("\"15.5\"".int("15.5")."<br>");
print("\"0155\"".int("0155")."<br>");
print("\"I'm 155\"".int("I'm
155")."<br>");
print("\"test\"".int("test")."<br>");
print("\"\"".int(""));
?>
The above returns:
155 TRUE
15.5 FALSE
"155" TRUE
"15.5" FALSE
"0155" TRUE
"I'm 155" FALSE
"test" FALSE
"" FALSE
----
Server IP: 213.249.64.162
Probable Submitter: 89.184.187.10
----
Manual Page -- http://www.php.net/manual/en/function.is-int.php
Edit -- https://master.php.net/note/edit/95658
Del: integrated -- https://master.php.net/note/delete/95658/integrated
Del: useless -- https://master.php.net/note/delete/95658/useless
Del: bad code -- https://master.php.net/note/delete/95658/bad+code
Del: spam -- https://master.php.net/note/delete/95658/spam
Del: non-english -- https://master.php.net/note/delete/95658/non-english
Del: in docs -- https://master.php.net/note/delete/95658/in+docs
Del: other reasons-- https://master.php.net/note/delete/95658
Reject -- https://master.php.net/note/reject/95658
Search -- https://master.php.net/manage/user-notes.php