SlinkyABC Posted October 28, 2011 Share Posted October 28, 2011 Hi, I did a search for this function and found a similar post but wasn't able to understand it. Presently I am just trying to do a simple test with the is_int function. I have it set up as follows: if (is_int ($variable)) { echo 'Integer'; } else { echo 'Not integer'; $variable is converted to a variable from some text on a web form. I can see the problem here is that no matter what is entered on the web form, $variable is always a string. The code I referenced above works fine if I instead use the is_string function. I guess I'm just trying to figure out the following: 1. Why is it always a string, even when entering a number such as 7? 2. How can I overcome this so that I can check to make sure the input is of the correct type (integer)? I know I'm probably not doing a good job of explaining what I mean but I think what I'm aiming to do should be clear; let me know if it isn't. NOTE--I think I get this to a degree--anything passed in a form will be a string, is that correct? So I should be able to use is_numeric instead. However, the problem with that is that it would also return True for non-integers such as floats. I only want the user to be able to enter integers, 8.5 should return False in this case. Thanks! Link to comment https://forums.phpfreaks.com/topic/249960-using-is_int/ Share on other sites More sharing options...
Nodral Posted October 28, 2011 Share Posted October 28, 2011 Try using this function function is_really_int(&$val) { $num = (int)$val; if ($val==$num) { $val=$num; return true; } return false; } Loads more information here http://php.net/manual/en/function.is-int.php Link to comment https://forums.phpfreaks.com/topic/249960-using-is_int/#findComment-1282920 Share on other sites More sharing options...
silkfire Posted October 28, 2011 Share Posted October 28, 2011 is_int will only return true if the variable has the internal type int, not if it's a mathematical integer. You should use some for of RegEx I think. Link to comment https://forums.phpfreaks.com/topic/249960-using-is_int/#findComment-1282954 Share on other sites More sharing options...
cyberRobot Posted October 28, 2011 Share Posted October 28, 2011 Have you looked into ctype_digit? http://php.net/manual/en/function.ctype-digit.php Link to comment https://forums.phpfreaks.com/topic/249960-using-is_int/#findComment-1282980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.