The faster way is:
if (strpos($haystack, $needle) !== false)
The case insensitive versions should obviouslly be slower (at least 2x slower, I expect).
strncmp()
/ substr()
can possibly perform better iff you're checking if $haystack
starts with $needle
and if $haystack
is considerably long (> hundreds chars or so).
Benchmark:
- strpos() vs. strncmp() = short | long
See other benchmarks @ http://net-beta.net/ubench/ (search for strpos
).
A pratical example where this kind of optimizations (kind of) do matter - calculating hashcashes:
$count = 0;
$hashcash = sprintf('1:20:%u:%s::%u:', date('ymd'), $to, mt_rand());
while (strncmp('00000', sha1($hashcash . $count), 5) !== 0)
{
++$count;
}
$header['X-Hashcash'] = $hashcash . $count;