Which is faster: in_array() or a bunch of expressions in PHP?

Posted by Darryl Hein on Stack Overflow See other posts from Stack Overflow or by Darryl Hein
Published on 2008-11-27T21:17:24Z Indexed on 2010/05/12 23:44 UTC
Read the original article Hit count: 151

Filed under:
|
|

Is it faster to do the following:

 if ($var != 'test1' && $var != 'test2' && $var != 'test3' && $var != 'test4') { ... }

Or:

 if (!in_array($var, array('test1', 'test2', 'test3', 'test4') { ... }

Is there a number of values at which point it's faster to do one or the other?

(In this case, the array used in the second option doesn't alreay exist.)

© Stack Overflow or respective owner

Related posts about php

Related posts about php-in-array