Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The !is_numeric function in PHP does not accept NULL VALUE

Tags:

php

mysql

I'm trying to validate a certain input wherein the user could just input integer values... otherwise an error message will be executed

$user_mcc = $_REQUEST['mobile_countrycode'];
if($user_mcc == ""){
    is_numeric($_REQUEST['mobile_countrycode']);
}

if (!is_numeric($_REQUEST['mobile_countrycode'])){

    echo '<script type="text/javascript">alert("Not a numeric value!\n\nMake sure that your country codes, area codes\nand mobile/fax/phone numbers are correct! \n"); return true;</script>';
    echo '<script type="text/javascript">history.back();</script>'; 
    die('' . mysql_error());



}

I've tried so many functions like empty , is_null , == NULL , == 'NULL and so on... but it didn't work.

If I put a string value to the input text field like let's say I input... "Banana" , the !is_numeric function above can be executed because the inputted value is FALSE and not a numeric value.

But, whenever I leave the input field empty which is NULL, the !is_numeric function can be still executed, like it recognizes a NULL value as not a numeric value. What can I do to bypass !is_numeric function if the input value is NULL. Thank you.

PS: I already tried !is_int , !is_integer and ctype_digit , but goes to same result, it did not accept NULL values.

like image 687
xirukitepe Avatar asked Oct 20 '25 16:10

xirukitepe


1 Answers

That's probably because null isn't a numeric value. It's void; it's nothing, and it certainly doesn't equal the integer 0. If you want to check for a numeric value, or null, then that's exactly what you should do:

if( $yourvalue !== null && !is_numeric( $yourvalue ) ) {
}
like image 169
Berry Langerak Avatar answered Oct 22 '25 05:10

Berry Langerak