Check that the value of $_GET is an Integer number

Asked

Viewed 456 times

1

I’m having trouble checking if the value of $_GET is whole. Below 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 on $_GET, he always keeps returning that he’s not a int.

3 answers

5

The method HTTP GET returns an array of strings passed by url through function urldecode. Then I suggest other forms of verification in case $_GET['PG'] be a number:

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

Or even only if there is the desired variable, using isset(), that for paging also works perfectly:

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

5


You can use the function is_numeric of PHP that checks if 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 numerical TRUE

3

In that case the

$_GET['PG']

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.