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

前端 未结 9 1507
北恋
北恋 2020-12-10 20:46

I have a page like so:

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

It has pagination links at the bottom by which we can br

相关标签:
9条回答
  • 2020-12-10 21:33
    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

    0 讨论(0)
  • 2020-12-10 21:35

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

    0 讨论(0)
  • 2020-12-10 21:38

    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;
    }
    
    0 讨论(0)
提交回复
热议问题