Reaper0167 Posted June 27, 2009 Share Posted June 27, 2009 is it good habbit to always use (int) for the examples below. $var = (int)$_GET['id']; $var = (int)$_SESSION['id']; Basically what I am asking is if there is a variable in the script and it is suppose to be a number, should I have the (int) in there for security purposes? Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/ Share on other sites More sharing options...
Andy-H Posted June 27, 2009 Share Posted June 27, 2009 Yes... Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/#findComment-864400 Share on other sites More sharing options...
natepizzle Posted June 27, 2009 Share Posted June 27, 2009 I don't think you need to cast the session id as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/#findComment-864404 Share on other sites More sharing options...
Reaper0167 Posted June 27, 2009 Author Share Posted June 27, 2009 That was kinda my question, thanks for the replies. Basically just when retrieving variables from the url then.? Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/#findComment-864407 Share on other sites More sharing options...
natepizzle Posted June 27, 2009 Share Posted June 27, 2009 POST too Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/#findComment-864409 Share on other sites More sharing options...
JasonLewis Posted June 27, 2009 Share Posted June 27, 2009 Well it depends, basically, the (int) is typecasting any variable to be an integer. So, if $_GET['id'] were to be a word, I'm pretty sure it would end up being 0. If you want to do error checking, your better off using is_numeric() to see if the variable is a number, or even is_int(). Check them both out and see which would suite you better. Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/#findComment-864411 Share on other sites More sharing options...
Andy-H Posted June 27, 2009 Share Posted June 27, 2009 I personally use it if the variable is storing user input which should be type integer, so if I use get data I do this: $int = (int)$_GET['id']; But if its not user inputted data it doesn't matter unless its logical to change a variables type like: $string = '3.147'; $int = (int)$string; $myInt = 100; Obviously with some logic in it too. Also I dont know if (int) assumes signed or unsigned int but i think it allows 0. Would be good to look into tho if you dont wish to allow negatives or w.e. Quote Link to comment https://forums.phpfreaks.com/topic/163827-solved-question-about-int/#findComment-864413 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.