slj90 Posted October 21, 2014 Share Posted October 21, 2014 I am trying to make it echo if a string doesn't equal any of the values. I have tried: if(!preg_match("/none|Blue|Brown|Green|Grey)/i", $eyecolor)){ echo "Not an eye color"; } and if($eyecolor !== "none" || $eyecolor !== "Blue" || $eyecolor !== "Brown" || $eyecolor !== "Green" || $eyecolor !== "Grey"){ $echo "Not an eye color"; } But these aren't working for what I am trying to do. Please help, Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/291963-if-string-doesnt-match-multiple-values/ Share on other sites More sharing options...
ginerjm Posted October 21, 2014 Share Posted October 21, 2014 (edited) Can't speak for the use of the pregmatch since I don't use it, but the second If statement I can. You do realize that that statement doesn't account for case? If the input is BLUE, then you are not going to match on it. I would suggest creating an array of your colors using uppercase values and then take your argument and do this: $colors = array('BLUE','GREEN','RED','YELLOW'); if ( in_array(strtoupper($mycolor), $colors)) echo "color found"; else echo "color NOT found"; Of course someone may coach you on your pregmatch and fix your problem, but the above will work too. PS your if statement also makes a common logical mistake. You need to check for AND, not OR, besides checking the case. (Go with the code I gave you - much less writing.) Edited October 21, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/291963-if-string-doesnt-match-multiple-values/#findComment-1494315 Share on other sites More sharing options...
requinix Posted October 21, 2014 Share Posted October 21, 2014 The preg_match() will fail because the expression has an error (there's an unbalanced closing parenthesis). You should actually be getting warnings about that, and if you aren't then it means you're not set up to see error messages. Which is very bad. Quote Link to comment https://forums.phpfreaks.com/topic/291963-if-string-doesnt-match-multiple-values/#findComment-1494346 Share on other sites More sharing options...
Psycho Posted October 21, 2014 Share Posted October 21, 2014 if($eyecolor !== "none" || $eyecolor !== "Blue" || $eyecolor !== "Brown" || $eyecolor !== "Green" || $eyecolor !== "Grey"){ $echo "Not an eye color"; } When you are doing NEGATIVE comparisons you have to be very careful about your AND/OR operators. No matter what the value is of $eyecolor that condition will always be TRUE. Let's just reduce that to two conditions if($eyecolor !== "none" || $eyecolor !== "Blue") Let's say the value of $eyecolor is "foo". That does not equal "none" and it does not equal "Blue", so the result of the comparison is TRUE. Now, let's say the value of $eyecolor is "Blue". Well, it doesn't equal "None" but it does equal "Blue". however, the result of that comparison is still TRUE! Because the comaprison is where the value does not equal "none" OR it does not equal "Blue". Since the first comparison is TRUE then the result is TRUE. The comparisons should use an AND to test for a value that does not match any of the value. But, a much simpler solution is to simply use an array of valid values. if(!in_array(strtolower($eyecolor), $validEyeColors)) { $echo "Not an eye color"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/291963-if-string-doesnt-match-multiple-values/#findComment-1494349 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.