Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25753

Is there any better way of defining if() condition using PHP?

Is there any better way of redefining this if(), what i dislike about this statement is $prefix is repeated again and again and it looks ugly to me.

if($prefix == 'RSVH' || 
   $prefix == 'RSAP' || 
   $prefix == 'CMOS' || 
   $prefix == 'CMSR' || 
   $prefix == 'CMKS' || 
   $prefix == 'CMWH' || 
   $prefix == 'CMBL' || 
   $prefix == 'LNRS' || 
   $prefix == 'LNCM' || 
   $prefix == 'LNMX' || 
   $prefix == 'PMNG');

thank you..

Upvotes: 4

Views: 85

Answers (2)

ComFreek
ComFreek

Reputation: 29434

You could use an array and the function in_array():

$values = array('RSVH', 'RSAP', 'CMOS' /*, ... */);

if ( in_array($prefix, $values) )
{
  /* do something */
}

Upvotes: 7

Michael Berkowski
Michael Berkowski

Reputation: 270687

Probably I would use in_array()

if (in_array($prefix, array('RSVH','RSAP','CMOS'.....))
{
   // It's in there...
}

Upvotes: 6

Related Questions