newbtophp Posted February 15, 2010 Share Posted February 15, 2010 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... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192159-which-is-the-most-suitable-function/ Share on other sites More sharing options...
premiso Posted February 15, 2010 Share Posted February 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192159-which-is-the-most-suitable-function/#findComment-1012679 Share on other sites More sharing options...
wildteen88 Posted February 15, 2010 Share Posted February 15, 2010 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/192159-which-is-the-most-suitable-function/#findComment-1012689 Share on other sites More sharing options...
newbtophp Posted February 15, 2010 Author Share Posted February 15, 2010 Thanks, both of you I'll be using the INT type cast, for sure. Quote Link to comment https://forums.phpfreaks.com/topic/192159-which-is-the-most-suitable-function/#findComment-1012695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.