Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 487
Ibrahim Azhar Armar Avatar asked Dec 13 '22 10:12

Ibrahim Azhar Armar


2 Answers

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

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

if ( in_array($prefix, $values) )
{
  /* do something */
}
like image 137
ComFreek Avatar answered May 13 '23 21:05

ComFreek


Probably I would use in_array()

if (in_array($prefix, array('RSVH','RSAP','CMOS'.....))
{
   // It's in there...
}
like image 20
Michael Berkowski Avatar answered May 13 '23 20:05

Michael Berkowski