Jump to content

Which is the most suitable function?


newbtophp

Recommended Posts

Which is the most suitable function, for validing the id (id is an auto increment in sql), is_int, ctype_digit, or is_numeric?

 

<?php

//always numbers...
$id = $_GET['id'];

if(is_int($id)){

//proceed...
}


if(ctype_digit($id)){

//proceed...

}


if(is_numeric($id)){

//proceed...

}

?>

 

:P

Link to comment
https://forums.phpfreaks.com/topic/192159-which-is-the-most-suitable-function/
Share on other sites

is_int would be my preference, but you could also just static cast the get data to an integer and be safe:

 

$id = (int)$_GET['id'];

 

Will force the get variable to be converted to an int. Either or should be fine.

 

EDIT:

Since you expect an INT, use that to check it. If you wanted any number, double int etc, then is_numeric would be the method of choice. Just an FYI, you check the exact type you are expecting, if only that type is allowed.

As you are getting the id from the url? All values in the query string are sent as strings. is_int will expect an integer value. So you're better of using is_numeric when checking to see if $_GET['id'] is a number. You'd then type cast $_GET['id'] (or use intval) to convert it to an integer. For example

 

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
    $id = (int) $_GET['id'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.