Jump to content

[SOLVED] Empty vs. Isset


lotrfan

Recommended Posts

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

Link to comment
Share on other sites

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?
    }

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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?

 

 

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.