lotrfan Posted October 15, 2007 Share Posted October 15, 2007 I have a user form where users input float values or integers. If I want to see if no value has been input, I use the empty() function. But if I want to see if users HAVE input a value that is a float or an integer, what function should I use? Thanks in Advance Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/ Share on other sites More sharing options...
hostfreak Posted October 15, 2007 Share Posted October 15, 2007 Look into the following: http://php.net/is_numeric http://www.php.net/manual/en/function.is-float.php http://www.php.net/manual/en/function.is-int.php Example: <?php $input = $_POST['input']; if (is_numeric($input)) { if (!is_float($input) || !is_int($input)) { //error msq } else { //do whatever since it is the input you want/ } } else { //input isn't even a number... something fishy... maybe log IP? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369608 Share on other sites More sharing options...
lotrfan Posted October 15, 2007 Author Share Posted October 15, 2007 Yes, I know, but if user inputs '00000123' I don't want that. I want some thing that can handle floats AND integers BUT ONLY numbers (with option of decimals of course). ex (taken from the page itself) <?php is_numeric('0123'); // true, not what I want is_numeric(0.123); // true is_numeric('0.123'); // true is_numeric(123); // true is_numeric('123'); // true is_numeric('foo'); // false ?> Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369613 Share on other sites More sharing options...
lotrfan Posted October 15, 2007 Author Share Posted October 15, 2007 Oh, just saw your edit... OK, so you think I need to change from $_REQUEST to $_POST? Why? And I'm going to have to use a combination of those functions to get the results I need? I thought perhaps there was a way without, but I will combine if I need to. Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369614 Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 all data input into your php from POST / get are strings. i.e: 123.45 will be a string of "123.45". So, you can not use is_float to check if it is a float. I would first use is_numeric to check if the string is a valid representation of a numeric value. then go thru the string to check if a period (.) exist. Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369615 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2007 Share Posted October 15, 2007 If they enter '0123' that is numeric and when you assign it to a variable, PHP will drop the leading zero. Be careful when using the empty() function on fields where you expect number to be entered. If a user enters a '0' (zero), the empty() function will return a "true". I find a safer way to determine whether a field has a value is to use the strlen() function. If that returns a zero, you know the field is truly empty. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369617 Share on other sites More sharing options...
lotrfan Posted October 15, 2007 Author Share Posted October 15, 2007 @hvle: So why not just stay with the $_REQUEST and go straight to is_numeric() then to is_float() and is_int()? @Ken: Using strlen(), I would see if string length is set to zero, right? Are values through $_REQUEST always set as srings? Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369618 Share on other sites More sharing options...
prime Posted October 15, 2007 Share Posted October 15, 2007 well you can use empty or !empty or !isset or isset Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369619 Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 here is a little trick for your problem: if (is_numeric($_POST['number'])) { $mynum = $_POST['number'] * 1; // you treated it as a number, php will convert to numeric } var_dump(is_float($mynum)); var_dump(is_int($mynum)); Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369632 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2007 Share Posted October 15, 2007 To see what happens when using the empty() function and the strlen() function, try this little script: <?php function test_empty($x) { if (empty($x)) echo 'The empty function says the string <span style="font-weight:bold;color:red">' . $x . '</span> is empty<br>'; if (strlen($x) > 0) echo 'The strlen function says the string <span style="font-weight:bold;color:blue">' . $x . '</span> is <span style="font-weight:bold">NOT</span> empty<br>'; echo '<pre>' . var_export($x,true) . '</pre>'; } test_empty('0'); test_empty(0); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369644 Share on other sites More sharing options...
lotrfan Posted October 15, 2007 Author Share Posted October 15, 2007 Thanks. I think I get it now. Quote Link to comment https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.