Jump to content

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'];
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.