| Method | Undefined | Null | False | Empty string | String "0" | String "1" | Long string | Summary | Index |
|---|---|---|---|---|---|---|---|---|---|
if (!$var) | 3.1 ms | 1.1 ms | 1.1 ms | 1.1 ms | 1.1 ms | 0.6 ms | 0.6 ms | 8.6 ms | 128 |
if (empty($var)) | 1.1 ms | 51.1 ms | 1.1 ms | 1.1 ms | 1.1 ms | 0.6 ms | 0.6 ms | 56.6 ms | 845 |
if ($var == "") | 3.2 ms | 1.2 ms | 271.2 ms | 1.2 ms | 0.9 ms | 0.9 ms | 2,454.5 ms | 2,732.9 ms | 40844 |
if ("" == $var) | 403.2 ms | 1.1 ms | 1.2 ms | 845.2 ms | 0.7 ms | 0.7 ms | 0.7 ms | 1,252.8 ms | 18723 |
if ($var === "") | 2.6 ms | 0.6 ms | 0.6 ms | 1.2 ms | 0.6 ms | 0.6 ms | 0.6 ms | 6.7 ms | 100 |
if ("" === $var) | 691.1 ms | 0.6 ms | 0.6 ms | 1.2 ms | 0.6 ms | 0.6 ms | 0.6 ms | 695.2 ms | 10390 |
if (strcmp($var, "") == 0) | 4.3 ms | 822.3 ms | 2.2 ms | 2.1 ms | 1.5 ms | 1.5 ms | 352.2 ms | 1,186.1 ms | 17726 |
if (strcmp("", $var) == 0) | 14.4 ms | 2.4 ms | 2.3 ms | 632.2 ms | 1.5 ms | 1.5 ms | 1.5 ms | 655.9 ms | 9802 |
if (strlen($var) == 0) | 4.0 ms | 182.0 ms | 2.0 ms | 1.8 ms | 1.3 ms | 1.3 ms | 1.3 ms | 193.5 ms | 2892 |
if (!strlen($var)) | 573.8 ms | 1.9 ms | 1.9 ms | 1.7 ms | 1.2 ms | 391.2 ms | 1.2 ms | 972.8 ms | 14539 |
My conclusion:
In most cases, use empty() because it does not trigger a warning when used with undefined variables.
Note that empty("0") returns true.
Use strlen() if you want to detect "0".
Try to avoid == at all because it may cause strange behaviour
(e.g. "9a" == 9 returns true).
Prefer === over == and !== over != if possible
because it does compare the variable types in addition to the contents.
| Method | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
|---|---|---|---|---|---|---|
strstr($haystack, $needle) | 1.2 ms | 2.1 ms | 1.7 ms | 1.8 ms | 6.8 ms | 100 |
strpos($haystack, $needle) !== false | 1.5 ms | 1,072.1 ms | 2.1 ms | 2.2 ms | 1,077.8 ms | 15903 |
strstr($haystack, $needle) !== false | 1.5 ms | 2.4 ms | 402.1 ms | 2.2 ms | 408.2 ms | 6023 |
stristr($haystack, $needle) | 2.9 ms | 3.8 ms | 313.5 ms | 3.6 ms | 323.8 ms | 4777 |
preg_match("/$needle/", $haystack) | 2.1 ms | 672.5 ms | 130.3 ms | 2.8 ms | 807.7 ms | 11918 |
preg_match("/$needle/i", $haystack) | 2.2 ms | 2.5 ms | 292.7 ms | 2.9 ms | 300.4 ms | 4432 |
preg_match("/$needle/S", $haystack) | 2.1 ms | 2.5 ms | 531.6 ms | 2.8 ms | 539.0 ms | 7953 |
ereg($needle, $haystack) | 2.9 ms | 53.3 ms | 650.1 ms | 106.9 ms | 813.3 ms | 12000 |
My conclusion:
It does not matter if you use strstr() or strpos().
Use the preg…() functions only if you need the power of regular expressions.
Never use the ereg…() functions.
| Method | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
|---|---|---|---|---|---|---|
strncmp($haystack, $needle, strlen($needle)) === 0 | 2.1 ms | 2.1 ms | 2.1 ms | 2.1 ms | 8.4 ms | 139 |
strncmp($haystack, "Test", 4) === 0 | 101.7 ms | 1.7 ms | 338.1 ms | 1.7 ms | 443.2 ms | 7269 |
strncasecmp($haystack, $needle, strlen($needle)) === 0 | 2.1 ms | 2.1 ms | 2.1 ms | 32.1 ms | 38.4 ms | 629 |
strpos($haystack, $needle) === 0 | 1.5 ms | 1.5 ms | 1.5 ms | 1.6 ms | 6.1 ms | 100 |
substr($haystack, 0, strlen($needle)) === $needle | 2.3 ms | 232.3 ms | 2.3 ms | 2.3 ms | 239.3 ms | 3924 |
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0 | 3.0 ms | 33.0 ms | 3.1 ms | 3.0 ms | 42.2 ms | 692 |
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack) | 45.0 ms | 5.0 ms | 114.9 ms | 4.9 ms | 169.9 ms | 2786 |
My conclusion:
strpos() is very fast and can be used in almost all cases.
strncmp() is good if you are looking for a constant length needle.
| Method | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
|---|---|---|---|---|---|---|
substr($haystack, strlen($haystack) - strlen($needle)) === $needle | 3.0 ms | 3.2 ms | 3.0 ms | 53.6 ms | 62.8 ms | 117 |
substr($haystack, -strlen($needle)) === $needle | 2.5 ms | 2.4 ms | 132.5 ms | 2.4 ms | 139.8 ms | 261 |
strcmp(substr($haystack, -strlen($needle)), $needle) === 0 | 3.3 ms | 3.7 ms | 43.3 ms | 3.3 ms | 53.5 ms | 100 |
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack) | 115.8 ms | 5.8 ms | 146.0 ms | 5.9 ms | 273.6 ms | 511 |
My conclusion:
Using substr() with a negative position is a good trick.
| Method | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
|---|---|---|---|---|---|---|
str_replace($search, $replace, $subject) | 3.0 ms | 3.6 ms | 3.2 ms | 1,073.2 ms | 1,083.0 ms | 2988 |
preg_replace("/$search/", $replace, $subject) | 3.7 ms | 24.3 ms | 4.1 ms | 4.1 ms | 36.2 ms | 100 |
preg_replace("/$search/S", $replace, $subject) | 45.5 ms | 4.2 ms | 4.1 ms | 4.1 ms | 58.0 ms | 160 |
ereg_replace($search, $replace, $subject) | 4.7 ms | 6.8 ms | 13.1 ms | 19.5 ms | 44.2 ms | 122 |
My conclusion:
Never use the ereg…() functions.
| Method | Not found | Found at start | Found at end | Found at both sides | Summary | Index |
|---|---|---|---|---|---|---|
trim($string, ",") | 0.4 ms | 0.4 ms | 0.4 ms | 0.4 ms | 1.8 ms | 100 |
preg_replace('/^,*|,*$/', "", $string) | 10.4 ms | 10.4 ms | 10.4 ms | 10.4 ms | 41.8 ms | 2383 |
preg_replace('/^,*|,*$/m', "", $string) | 16.6 ms | 16.6 ms | 262.9 ms | 151.8 ms | 447.9 ms | 25569 |
preg_replace('/^,+|,+$/', "", $string) | 0.8 ms | 0.8 ms | 0.8 ms | 0.8 ms | 3.1 ms | 178 |
preg_replace('/^,+|,+$/m', "", $string) | 0.8 ms | 0.8 ms | 40.8 ms | 0.8 ms | 43.2 ms | 2464 |
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …)) | 1.2 ms | 1.2 ms | 1.2 ms | 1.2 ms | 4.9 ms | 280 |
My conclusion:
Always benchmark your regular expressions!
In this case, with .* you also replace nothing with nothing which takes time
because there is a lot of “nothing” in every string.
| Method | Empty string | Single occurrence | Multiple occurrences | Summary | Index |
|---|---|---|---|---|---|
explode(",", $string) | 1.5 ms | 1.8 ms | 213.8 ms | 217.2 ms | 766 |
split(",", $string) | 1.6 ms | 351.8 ms | 1,481.5 ms | 1,834.9 ms | 6474 |
preg_split("/,/", $string) | 1.9 ms | 2.4 ms | 24.1 ms | 28.3 ms | 100 |
preg_match_all('/[^,]+/', $string, $matches) | 2.8 ms | 4.5 ms | 28.3 ms | 35.6 ms | 126 |
My conclusion:
Don't use split(). It's deprecated in PHP 5.3 and will be removed in PHP 6.
| Method | Summary | Index |
|---|---|---|
for ($i = 0; $i < count($array); $i++) | 95.0 ms | 4483 |
for ($i = 0, $count = count($array); $i < $count; $i++) | 3.5 ms | 164 |
for ($i = count($array) - 1; $i >= 0; $i--) | 2.7 ms | 128 |
for ($i = count($array) - 1; $i >= 0; --$i) | 2.6 ms | 123 |
$i = count($array); while ($i--) | 2.1 ms | 100 |
My conclusion:
count() is horribly slow. Always precalculate it, if possible.
| Method | Summary | Index |
|---|---|---|
'contains no dollar signs' | 1.8 ms | 101 |
"contains no dollar signs" | 1.8 ms | 101 |
'$variables $are $not $replaced' | 1.8 ms | 100 |
"\$variables \$are \$not \$replaced" | 1.9 ms | 109 |
"$variables $are $replaced" | 16.1 ms | 909 |
$variables . ' ' . $are . ' ' . $replaced | 22.9 ms | 1292 |
$variables . " " . $are . " " . $replaced | 22.3 ms | 1258 |
My conclusion: It does not matter if you use single or double quotes at all. The inclusion of variables has a measurable effect, but that's independent from the quotes.
© Thiemo Mättig, created in September 2008, updated in March 2010