j.smith1981 Posted May 6, 2011 Share Posted May 6, 2011 Hi there, I was thinking of using this following check system to see if a user has entered a good valid value for a categories selection, but it is just is_numeric, here's my line of single code for this: if(!array_key_exists('category', $_GET) || trim(is_numeric($_GET['category'])) == '') Is there anything better that I can be using than is_numeric()? I mean this would allow 2.22 which for the categories selection, would not be valid, I will only be allowing integer values, I mean it would potentially come up with no category selected but that wouldn't be very good would it? It could be applied to a blog I am doing aswell, any guidance on such an improvement on this function would very much be appreciated, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/ Share on other sites More sharing options...
prestonwinfrey Posted May 6, 2011 Share Posted May 6, 2011 Use is_int() to validate an integer. Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211450 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 Use is_int() to validate an integer. Problem is is_int("10") would return false and GET is returning a string Maybe if(isset($_GET['category']) && ((int)$_GET['category'])==$_GET['category']){ $category = (int)$_GET['category']; //Logic is convert to int and compare to unconverted, //as 10 == "10" is true //ALSO 10 == "10.0" would be true //BUT 10 == "10.2" would be false echo "true = $category"; } Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211451 Share on other sites More sharing options...
prestonwinfrey Posted May 6, 2011 Share Posted May 6, 2011 Use is_int() to validate an integer. Problem is is_int("10") would return false and GET is returning a string Maybe if(isset($_GET['category']) && ((int)$_GET['category'])==$_GET['category']){ } Could you cast the variable as an integer and then validate using is_int() or would that return true regardless because of the cast? Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211452 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 Normally i would do this $category = (int)$_GET['category']; if(!empty($category)){ //BUT if category is 0 then it will fail, but i use it main for Database ID's } Could you cast the variable as an integer and then validate using is_int() or would that return true regardless because of the cast? Na, converting to an INT would make the validation pointless as it would be an int no matter what it was before hand Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211454 Share on other sites More sharing options...
j.smith1981 Posted May 6, 2011 Author Share Posted May 6, 2011 Ahh I forgot about casting, thanks! Kind of for a millisecond then forgot that all super global variables are always strings, but then I did use is_numeric, but casting it to an (int) is a much better idea, I don't personally see a point in comparing it, because if the user was to say put in (off the top of my head) 2.22 ah I see your point now! Could just output a message if their different then say you entered some invalid data, yes will do that! Sorry just thinking outloud, but yes thank you ever so much! Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211460 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 LOL. no worries.. i do that as well.. So erm.. Solved ? Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211464 Share on other sites More sharing options...
j.smith1981 Posted May 6, 2011 Author Share Posted May 6, 2011 In a nutshell yes, now just getting my other logic sorted but I understand what I am doing now to get an error message to the user as such if the data entered is not valid. It's sometimes best I feel on occasions to go over it again and again so it firmly sticks in my head. I mean the lower parts of my logic anyways it would say you haven't selected a category, but should show some error checking and that is why I was really asking to be fair. Just says to me (being that I used to be a user, but got interested in Programming logic, what sort of got me into doing PHP work and the like), is that what data I did enter was valid, just no category exists for it, but to stop it from querying the database should any invalid data be attempted to be entered, if that makes any sense? Sorry just my explanations need sometimes a bit of work, but I bet you get my point? Thank you ever so much though! Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211485 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 I know what you mean, as for error messages, I use my default message if($anything!=true){ echo "User Error"; } Short simple and true and now i can charge 50p and minute for telephone support (just kidding) Quote Link to comment https://forums.phpfreaks.com/topic/235701-checking-for-valid-data-question/#findComment-1211503 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.