maxxon15
maxxon15

Reputation: 1639

How to check whether a variable in $_GET Array is an integer?

I have a page like so:

http://sitename/gallery.php?page=2

It has pagination links at the bottom by which we can browse. Everytime the page numbers are clicked, it would send a GET request with parameters page=1 or page=2 and so on ...

When I store these values to $page from teh $_GET variable, it is a string value. I can convert it to an integer using (int) like this:

if(!empty($_GET['page'])){
       $page = (int)$_GET['page'];
       echo "Page Number: ".$page;
}

But how can I make sure that the value passed is an integer only and not other crap?

Upvotes: 4

Views: 9963

Answers (9)

Jonathan Falkner
Jonathan Falkner

Reputation: 410

if (isset($_GET['page']) && (($get_page_filtered = filter_var($_GET['page'], FILTER_VALIDATE_INT)) !== FALSE) {
  $get_page_int = $get_page_filtered;
}

@see https://stackoverflow.com/a/41868665/6758130

Upvotes: 0

Techn0Mania
Techn0Mania

Reputation: 11

this is a way how to check parameter if it is intetger or not.

if (is_int((int) $_GET['user_id']) && (int) $_GET['user_id'] != 0) {
    $user_id = $_GET['user_id'];
}

Upvotes: 1

GautamD31
GautamD31

Reputation: 28763

You can also check with

isNAN($_GET['something']);//is_numeric($_GET['something'])

it returns a boolean value(true,flase)....if its true then it is not an integer,if false its an integer.

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

I've left a few comments here and there. Thanks to weak typing, functions like empty and isset tend to be unreliable. The quickest way to check if a parameter is an int or not IMO would be this:

if (array_key_exists('page',$_GET) && ($_GET['page'] == (int) $_GET['page']))

Casting to int and then compare the respective values will return true only when $_GET['page'] is a valid int. If you want to use strict type comparison for some reason (some do), you could double cast:

if (array_key_exists('page',$_GET) && ($_GET['page'] === (string)((int) $_GET['page'])))

But in this particular case, I can't really see why you would want to do that

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173542

Using filters:

if (null !== ($page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE))) {
    // $page is now an integer
}

This also checks whether the variable appears in the query string at the same time. If you want to differentiate between missing and invalid you have to leave off the last argument to filter_input():

$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
// $page can be null (not present), false (present but not valid) or a valid integer

Upvotes: 17

Leonel Machava
Leonel Machava

Reputation: 1531

Use is_numeric().

is_int() will not work because GET parameters are always string.

Upvotes: 2

GeoffreyB
GeoffreyB

Reputation: 1839

if(!empty($_GET['page']) and is_numeric($_GET['page'])){
       $page = (int)$_GET['page'];
       echo "Page Number: ".$page;
}

is_numeric is probably what you need.

Upvotes: 0

Berry Langerak
Berry Langerak

Reputation: 18859

Using is_int won't help, probably. All incoming parameters (including $_GET and $_POST) are parsed as strings by PHP. The is_int function checks the datatype, not the value. ctype_digit checks for only digits though:

if(isset($_GET['page']) && ctype_digit($_GET['page']){
   $page = (int)$_GET['page'];
   echo "Page Number: ".$page;
}

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Use filter_var() with the FILTER_VALIDATE_INT filter on it, and check the return value.

Upvotes: 4

Related Questions