Verify that the value of $ _GET is a Integer

2

I'm having trouble checking if the value of $_GET is integer. Here is the code:

<?php
     $modo=$_GET['PG'];
     if(is_int($modo)){
         echo "e inteiro";
     }else{
         echo "n e inteiro";
     }
?>

Even though I put a number in $_GET , it always returns that it is not a int .

    
asked by anonymous 04.06.2018 / 15:39

3 answers

5

You can use the is_neric function of PHP that checks whether a variable is a number, even if it comes as a string.

But it has to be noted that it will check from Hexadecimal to Real and binary numbers as numeric TRUE

    
04.06.2018 / 15:47
6

The HTTP GET method returns an array of strings passed by url through the function urldecode . So I suggest other forms of verification, in case% of% is a number:

if(is_numeric($_GET['PG']))

Or even only if the desired variable exists, using $_GET['PG'] , which for paging also works perfectly:

if(isset($_GET['PG']))
    
04.06.2018 / 15:47
3

In this case

$_GET['PG']

It returns a string so it is always false ...

    
04.06.2018 / 15:43