Checking if a variable is an integer in PHP

Viewed 135702

I have the following code

    $page = $_GET['p'];

    if($page == "")
    {
        $page = 1;
    }
    if(is_int($page) == false)
    {
        setcookie("error", "Invalid page.", time()+3600);
        header("location:somethingwentwrong.php");
        die();
    }
    //else continue with code

which I am going to use for looking at different "pages" of a database (results 1-10, 11-20, etc). I can't seem to get the is_int() function to work correctly, however. Putting "1" into the url (noobs.php?p=1) gives me the invalid page error, as well as something like "asdf".

13 Answers

When i start reading it i did notice that you guys forgot about abvious think like type of to check if we have int, string, null or Boolean. So i think gettype() should be as 1st answer. Explain: So if we have $test = [1,w2,3.45,sasd]; we start test it

foreach ($test as $value) {
        $check = gettype($value);
        if($check == 'integer'){
            echo "This var is int\r\n";
        }
        if($check != 'integer'){
            echo "This var is {$check}\r\n";
        }
}

And output:

> This var is int 
> This var is string 
> This var is double 
> This var is string

I think this is easiest way to check if our var is int, string, double or Boolean.

An integer starting with 0 will cause fatal error as of PHP 7, because it might interpret it as an octal character.

Invalid octal literals

Previously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error.

So you might want to remove leading zeros from your integer, first:

$var = ltrim($var, 0);

In my opinion the correct function should look as following:

    public function checkIfValueIsInt($value): bool
{
    if (is_bool($value)) {
        return false;
    }

    if (0 === filter_var($value, FILTER_VALIDATE_INT) || filter_var($value, FILTER_VALIDATE_INT)) {
        return true;
    }

    return false;
}

given set of tests is validated correctly using it:

        [
            true,
            false,
        ],
        [
            false,
            false,
        ],
        [
            'false',
            false,
        ],
        [
            'true',
            false,
        ],
        [
            'some random string',
            false,
        ],
        [
            [],
            false,
        ],
        [
            ['some string', 1],
            false,
        ],
        [
            (object) [],
            false,
        ],
        [
            2.222,
            false,
        ],
        [
            rand(0, 99999),
            true,
        ],
        [
            1,
            true,
        ],
        [
            0,
            true,
        ],
        [
            -1,
            true,
        ],

the reason I added is_bool check is that

true

value is handle incorrectly by:

0 === filter_var($value, FILTER_VALIDATE_INT) || filter_var($value, FILTER_VALIDATE_INT)
Related