开发者

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

Is there any better way开发者_如何学C 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..


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

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

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


Probably I would use in_array()

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜