views:

511

answers:

5

Hey is this a possible funtion?

I need to check if a variable is existent in a list of ones I need to check against and also that cond2 is true eg

if($row['name'] == ("1" || "2" || "3") && $Cond2){
    doThis();
}

It's not working for me and all I changed in the copy paste was my list and the variable names

A: 
$name = $row['name'];
if (($name == "1" || $name == "2" || $name == "3") && $cond2)
{
  doThis();
}
Eric Hogue
that's a possibility but the others are smarter
Supernovah
I agree, in_array is a better way to do this.
Eric Hogue
+4  A: 
if(in_array($row['name'], array('1', '2', '3')) && $Cond2) {
  doThis();
}

PHP's in_array() docs: http://us.php.net/manual/en/function.in-array.php

ceejayoz
Thanks. Accepted as it was the first
Supernovah
Just as a minor side note, depending on how big your "exceptions-array" is you might wanna put $Cond2 as the first element of the boolean expression (assuming it is already atomic and not some sort of potentially expensive function call).
n3rd
Good point, n3rd.
ceejayoz
+2  A: 

You're lookin for the function in_array().

if (in_array($row['name'], array(1, 2, 3)) && $cond2) {
    #...
soulmerge
+1  A: 

use in_array function if(in_array($row['name'], array(1,2,3)) && $cond2){ do ... }

Haim Evgi
+1  A: 
if (in_array($name , array( 'Alice' , 'Bob' , 'Charlie')) && $condition2 ) {
 /* */
}
garrow