1

This is the PHP code and I want to check if an invalid value is present:

$response = $_POST['response'];
$visibility = $_POST['visibility'];


if($response == NULL || $visibility == NULL ){
    printf("Invalid input: %s\n", mysqli_connect_error());
    echo "<br/><a href='myevents.php'>Back to previous page</a>";
    exit();
}

$response and $visibility should be the integer value so If people put the string value I want to go to the if($response == NULL || $visibility == NULL ) statement. How to write the statement $response == ???

3 Answers 3

1

Using is_numeric will provide the desired result. is_numeric finds whether the given variable is numeric

if (!is_numeric($response) || is_numeric($visibility))
 echo "Invalid input";

Some people suggest is_int(). Don’t use is_int(). Use is_numeric() instead.

Copy the following chunk of code into a php file and run it. You’ll be surprised at the outcome:

$t = "12345";
if( is_int($t ) ) {
    echo $t . " is an int!";
} else {
    echo $t . " is not an int!";
}

The problem is that is_int() thinks a string of numbers is a string, not an integer.

The key difference between the two is the one checks the type of variable, is_int(), and the other checks the value of the variable, is_numeric().

Sign up to request clarification or add additional context in comments.

2 Comments

How is there some function for string value?
@Lee what do you want exactly
0

Can you try using is_nan() function,

if(is_nan($response) || is_nan($visibility)){
    printf("Invalid input: %s\n", mysqli_connect_error());
    echo "<br/><a href='myevents.php'>Back to previous page</a>";
    exit();
}

var_dump(NAN == NAN); // boolean true
var_dump(NAN === NAN); // boolean true
var_dump(is_nan(NAN)); // boolean true

var_dump(NAN == 12); // boolean true
var_dump(NAN === 12); // boolean false
var_dump(is_nan(12)); // boolean false

var_dump(NAN == 12.4); // boolean true
var_dump(NAN === 12.4); // boolean true
var_dump(is_nan(12.4)); // boolean false

var_dump(NAN == NULL); // boolean true
var_dump(NAN === NULL); // boolean false
var_dump(is_nan(NULL)); // boolean false

var_dump(NAN == 'K<WNPO'); // boolean true
var_dump(NAN === 'K<WNPO'); // boolean false
var_dump(is_nan('K<WNPO')); // null and throws a warning "Warning: is_nan() expects parameter 1 to be double, string given in NANTest.php on line 13"

Ref: http://www.php.net/manual/en/function.is-nan.php

Comments

0

try is_numeric or is_int

Follow these links:

http://php.net/is_int

http://www.php.net/is_numeric

1 Comment

Nope. is_int() finds whether the type of the given variable is integer. To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.