Monkuar Posted January 23, 2015 Share Posted January 23, 2015 (edited) if($value != "0"){ $errors[] = 'Invalid option chosen'; } $value is a string. IF $value equals '00', it doesn't work why? It does not equal 0? I'm trying to check if a value equalis 1 or 0. LITERALLY the string has to be 1 or 0, if not I need to error out. Why is this so difficult? How does 2 zero's (00) = 0? Doesn't make sense. Especially when it's matching 2 STRINGs.. If it were a numeric value I could see PHP saying 00 might equal 0. But how does 00 and 0 equal the same thing? makes no sense. Edited January 23, 2015 by Monkuar Quote Link to comment Share on other sites More sharing options...
Solution scootstah Posted January 23, 2015 Solution Share Posted January 23, 2015 You should use strict type comparison. if ($value !== '0')This will look for string '0'. 1 Quote Link to comment Share on other sites More sharing options...
Monkuar Posted January 23, 2015 Author Share Posted January 23, 2015 (edited) You should use strict type comparison. if ($value !== '0')This will look for string '0'. BOOM if($value !== "0" AND $value !== "1"){ $errors[] = 'Invalid option chosen'; }Got it now. So the word 'strict' is what I was looking for, thank you scootstah! Now these values HAVE TO BE 1 or 0, no matter what. No injection, nothing is possible. Thank you. Going to array map intval these too just in case. Edited January 23, 2015 by Monkuar Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 23, 2015 Share Posted January 23, 2015 Going to array map intval these too just in case. Well, then it won't work. Then your value is an int but you're comparing it to a string. Also, if your value was a non-int string before hand then it will be converted to int 0, which might be breaking your logic. Quote Link to comment Share on other sites More sharing options...
Monkuar Posted January 23, 2015 Author Share Posted January 23, 2015 (edited) Well, then it won't work. Then your value is an int but you're comparing it to a string. Also, if your value was a non-int string before hand then it will be converted to int 0, which might be breaking your logic. Yeah, I just got some false positives. I would have to switch back to != if they were integers right? Anyways, I'm not going to array map them, as this a very strict match. No need I just find it sad I spent like 1 hour trying to figure this out, I feel like absolute shit and should be ashamed. Edited January 23, 2015 by Monkuar Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 23, 2015 Share Posted January 23, 2015 Yeah, I just got some false positives. I would have to switch back to != if they were integers right? No, just change your comparison to an int as well. if ($value !== 0) I just find it sad I spent like 1 hour trying to figure this out, I feel like absolute shit and should be ashamed. Ha, trust me we've all been there. We've all been stuck on stupid little problems like this. Sometimes you just need to walk away and do something else for a bit. 1 Quote Link to comment Share on other sites More sharing options...
kicken Posted January 24, 2015 Share Posted January 24, 2015 If you want to know why it does what it does when using !=, see the manual on comparisons, specifically: If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. Basically if both strings consist of something that is_numeric says is a number, PHP will convert it to a number then compare them. So '00' become (int)0 and '0' becomes (int)0. 0 != 0 is false. 1 Quote Link to comment 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.