Click to See Complete Forum and Search --> : if some thing is


lomokev
04-25-2006, 03:51 AM
is there a quwicker way of writeing with out wirteing $string over and over again

if ($string == "a" || $string == "t" || $string == "d" ||){

}

or

$varArray = array("a","t","d');
for ($n=0;$n<count($varArray);$n++){
if ($string == $varArray[$n]){
//do some thing!
brake;
}
}

aaronbdavis
04-25-2006, 07:15 AM
use the in_array (http://us3.php.net/in_array) function.
e.g.

$arr = array('s', 'd', 't');
if (in_array($string, $arr))
{
/* do something */
}

NogDog
04-25-2006, 09:06 AM
Or, you can do it in one line as:

if(in_array($string, array('s', 'd', 't')))
{
// do something
}

Or, you could use preg_match:

if(preg_match('/^[sdt]$/', $string))
{
// do something
}

lomokev
04-25-2006, 04:39 PM
cool i have seen strings like that before have know clue what they are

waht dose the /^ and $/ mean?

Daniel T
04-25-2006, 05:42 PM
if(preg_match('/^[sdt]$/', $string))
{
// do something
}

/ and / designate the beginning and end of a Regular Expression. ^ represents the beginning of the string, $ represents the end. [sdt] is one character, either an s, d, or t. So in order for it to be a match, the string would have to be exactly s, d, or t.