Checking a value within a list

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
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Checking a value within a list

Post by hame22 »

Hi

I am looking for a way in which I can check a variable is in a certain list of values.

What i am doing is taking a country then checkin whether it is in a certain group e.g. continent etc.

Now i know i can do this:

Code: Select all

if ($cn = UK || $cn = SP || $cn = FR || $cn =GER || ..etc.....

print ' European country';

but i wondered whether there is a quicker way to achieve this in code

Thanks in advance
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Put all country abbreviations in an array, then call in_array() to check:

Code: Select all

$eu = array("UK","SP","FR","SK");
if(in_array($cn,$eu))
{
   echo "European Country";
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

awesome exactly what i was after, thanks!!
Post Reply