Compare a var to two or more values.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Compare a var to two or more values.

Post by JellyFish »

How do I compare $var to two or more values?

E.g:

Code: Select all

if ($var != "this" && "this")
This obviously doesn't work or then I wouldn't be asking this question. :D

So how do I do this:

Code: Select all

if (($var != "this") && ($var != "that"))
Only in a more compact way, so to speak.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g.

Code: Select all

if ( !in_array($var, array('x','y','z')) )
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Thanks man. It works like a charm. :D

If there are any other suggestions that might be shorter this topic is still open.
Post Reply