How do I write a function that returns FALSE if a given string is NOT A VALID NUMBER OF TYPE [PHP INT], and returns TRUE otherwise.
This is simple in other languages.
intval(), isint(), and is_numeric() are not adequate, here is why:
is_numeric() is not adequate because it matches to any number not just integers, also it accepts huge numbers as numeric, which aren't integers. intval() is not adequate because it returns 0 for BOTH invalid PHP integers like '9000000000000000' AND the valid PHP integer '0' or '0x0' or '0000' etc. isint() only tests if a variable is already of type int, it doesn't deal with strings or conversion to int.
Maybe there is a popular library for this or something?
I want to call a function that is capable of detecting whether the form data someone posts is a valid php integer, for instance.
I want to call the function that does this: is_php_integer($str_test_input). What goes in the function?
<?php
$strInput = 'test' //function should return FALSE
$strInput = '' //function should return FALSE
$strInput = '9000000000000000' //function should return FALSE since
//is not valid int in php
$strInput = '9000' //function should return TRUE since
//valid integer in php
$strInput = '-9000' // function should return TRUE
$strInput = '0x1A' // function should return TRUE
// since 0x1A = 26, a valid integer in php
$strInput = '0' // function should return TRUE, since
// 0 is a valid integer in php
$strInput = '0x0' // function should return TRUE, since
// 0x0 = 0 which is a valid integer in php
$strInput = '0000' // function should return TRUE, since
// 0000 = 0 which is a valid integer in php
function is_php_integer($strTestInput) {
// what goes here?
// ...
// if string could be interpreted as php integer, return true
// else, return false
}
if is_php_integer($strInput) {
echo 'your integer plus one equals: '. (intval($strInput) + 1);
} else {
echo 'your input string is not a valid php integer'
}
?>
Thanks in advance!