Jump to content

[SOLVED] Empty vs. Isset


lotrfan

Recommended Posts

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
https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369608
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
https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369613
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
https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369615
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
https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369617
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
https://forums.phpfreaks.com/topic/73263-solved-empty-vs-isset/#findComment-369644
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.