Jump to content

If string doesnt match multiple values


slj90

Recommended Posts

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.

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

 

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";
}
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.