Forum Moderators: coopster

Message Too Old, No Replies

Help using IF Statements and Arrays

Its a bit too basic for me ;-)

         

chriswragg

5:37 pm on May 5, 2005 (gmt 0)

10+ Year Member



Sorry I'm a newbie to PHP, so this question is probably very simple.

What i basically want to do is use IF to see if my variable appears in group of strings.

For Example to find out whether Yellow appears in the following list:
Black
Blue
Red
Brown
Green

My initial though is an array of strings, but I am a bit unsure of how to do this.

Any help would be appreciated

Thanks Chris

mooseh

5:58 pm on May 5, 2005 (gmt 0)

10+ Year Member



Here's a way that would let you do it:
<?
// create an array of strings called $colours:
$colours = array( "Black", "Orange", "Red", "Brown", "Green" );
// use the in_array() function to check if "Yellow" is in the array:
if( in_array("Yellow", $colours) )
{
echo "Yellow is in the array";
}
else
{
echo "Yellow isn't in the array";
}
?>

It creates an array with various strings, then uses the in_array() function to check if the string "Yellow" is in the array.

chriswragg

6:17 pm on May 5, 2005 (gmt 0)

10+ Year Member



Cheers that works ;)

Chris