I just want to highlight that (at least on php7), when testing for the existence of a string in the beginning of another string you should consider using substr or strpos (if performances is an issue).
Here is a small benchmark (for what it's worth):
<?php
$n = 'abcd';
$l = strlen($n);
$haystack0 = base64_encode(random_bytes(128));
$r = 1;
for ($i = 0; $i < 100000000; $i++)
$r += $r * $r % 10000;
$k = 30000000;
$res = array();
foreach (array('found' => $n . $haystack0, 'not-found' => strrev($n) . $haystack0) as $f => $haystack) {
$m = microtime(true);
for ($i = 0; $i < $k; $i++)
!strncmp($haystack, $n, $l) && $r++;
$res["strncmp-$f"] = -$m + ($m = microtime(true));
for ($i = 0; $i < $k; $i++)
(strpos($haystack, $n) === 0) && $r++;
$res["strpos-$f"] = -$m + ($m = microtime(true));
for ($i = 0; $i < $k; $i++)
(substr($haystack, 0, $l) === $n) && $r++;
$res["substr-$f"] = microtime(true) - $m;
}
asort($res);
print_r($res);
echo "\n$r"; ?>
This outputs:
<?php ?>