| Method | Undefined | Null | False | Empty string | String "0" | String "1" | Long string | Summary | Index |
|---|---|---|---|---|---|---|---|---|---|
if (!$var) | 2.4 ms | 0.4 ms | 0.4 ms | 0.4 ms | 0.4 ms | 0.2 ms | 0.2 ms | 4.3 ms | 136 |
if (empty($var)) | 0.4 ms | 0.5 ms | 0.5 ms | 0.5 ms | 0.5 ms | 0.3 ms | 0.3 ms | 3.1 ms | 100 |
if ($var == "") | 2.6 ms | 0.4 ms | 0.4 ms | 0.5 ms | 0.5 ms | 0.5 ms | 38.0 ms | 43.0 ms | 1371 |
if ("" == $var) | 2.6 ms | 0.5 ms | 0.5 ms | 0.5 ms | 0.3 ms | 0.3 ms | 0.4 ms | 5.1 ms | 161 |
if ($var === "") | 2.1 ms | 0.2 ms | 0.2 ms | 0.5 ms | 0.2 ms | 0.2 ms | 0.2 ms | 3.5 ms | 111 |
if ("" === $var) | 2.1 ms | 0.2 ms | 0.2 ms | 0.4 ms | 0.2 ms | 0.2 ms | 0.2 ms | 3.5 ms | 112 |
if (strcmp($var, "") == 0) | 3.7 ms | 1.5 ms | 1.8 ms | 1.3 ms | 1.2 ms | 1.6 ms | 1.1 ms | 12.3 ms | 391 |
if (strcmp("", $var) == 0) | 3.8 ms | 1.6 ms | 1.7 ms | 1.3 ms | 1.1 ms | 1.1 ms | 1.1 ms | 11.8 ms | 375 |
if (strlen($var) == 0) | 3.4 ms | 1.2 ms | 1.3 ms | 1.0 ms | 0.8 ms | 0.8 ms | 0.8 ms | 9.4 ms | 301 |
if (!strlen($var)) | 3.2 ms | 1.1 ms | 1.3 ms | 0.9 ms | 0.7 ms | 0.8 ms | 0.7 ms | 8.8 ms | 280 |
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 | Equal | First character not equal | Last character not equal | Summary | Index |
|---|---|---|---|---|---|
$a == $b | 2.1 ms | 1.4 ms | 1.9 ms | 5.3 ms | 100 |
!strcmp($a, $b) | 3.9 ms | 3.2 ms | 3.8 ms | 10.8 ms | 203 |
strcmp($a, $b) == 0 | 3.9 ms | 3.3 ms | 3.9 ms | 11.2 ms | 210 |
strcmp($a, $b) === 0 | 3.7 ms | 3.5 ms | 3.8 ms | 11.0 ms | 205 |
strcasecmp($a, $b) === 0 | 8.0 ms | 3.1 ms | 8.0 ms | 19.0 ms | 356 |
My conclusion: ?
| Method | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
|---|---|---|---|---|---|---|
strstr($haystack, $needle) | 1.0 ms | 1.0 ms | 1.1 ms | 1.1 ms | 4.2 ms | 102 |
strpos($haystack, $needle) !== false | 0.9 ms | 1.0 ms | 1.1 ms | 1.1 ms | 4.1 ms | 100 |
strstr($haystack, $needle) !== false | 0.9 ms | 1.1 ms | 1.2 ms | 1.2 ms | 4.4 ms | 108 |
stristr($haystack, $needle) | 1.8 ms | 2.2 ms | 2.8 ms | 2.1 ms | 8.9 ms | 217 |
preg_match("/$needle/", $haystack) | 1.9 ms | 1.8 ms | 2.0 ms | 2.2 ms | 7.9 ms | 194 |
preg_match("/$needle/i", $haystack) | 1.9 ms | 1.9 ms | 2.1 ms | 2.3 ms | 8.2 ms | 200 |
preg_match("/$needle/S", $haystack) | 1.9 ms | 1.8 ms | 2.1 ms | 2.2 ms | 8.0 ms | 196 |
ereg($needle, $haystack) | 2.2 ms | 2.2 ms | 9.5 ms | 16.8 ms | 30.6 ms | 748 |
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 | 1.5 ms | 1.4 ms | 1.4 ms | 1.4 ms | 5.7 ms | 149 |
strncmp($haystack, "Test", 4) === 0 | 1.0 ms | 1.0 ms | 1.0 ms | 1.0 ms | 4.0 ms | 103 |
strncasecmp($haystack, $needle, strlen($needle)) === 0 | 1.4 ms | 1.4 ms | 1.4 ms | 1.4 ms | 5.6 ms | 146 |
strpos($haystack, $needle) === 0 | 1.0 ms | 0.9 ms | 1.0 ms | 1.0 ms | 3.8 ms | 100 |
substr($haystack, 0, strlen($needle)) === $needle | 1.6 ms | 1.6 ms | 1.6 ms | 1.6 ms | 6.4 ms | 165 |
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0 | 2.2 ms | 2.2 ms | 2.3 ms | 2.2 ms | 8.9 ms | 232 |
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack) | 2.9 ms | 3.1 ms | 2.9 ms | 2.9 ms | 11.8 ms | 306 |
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 | 2.0 ms | 2.0 ms | 2.3 ms | 2.3 ms | 8.5 ms | 136 |
substr($haystack, -strlen($needle)) === $needle | 1.6 ms | 1.6 ms | 1.6 ms | 1.6 ms | 6.3 ms | 100 |
strcmp(substr($haystack, -strlen($needle)), $needle) === 0 | 2.2 ms | 2.2 ms | 2.2 ms | 2.2 ms | 8.8 ms | 141 |
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack) | 3.3 ms | 3.7 ms | 3.6 ms | 3.7 ms | 14.2 ms | 227 |
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) | 1.8 ms | 1.9 ms | 1.8 ms | 1.9 ms | 7.5 ms | 100 |
preg_replace("/$search/", $replace, $subject) | 2.9 ms | 3.2 ms | 3.4 ms | 3.3 ms | 12.8 ms | 171 |
preg_replace("/$search/S", $replace, $subject) | 2.7 ms | 3.3 ms | 3.3 ms | 3.2 ms | 12.5 ms | 167 |
ereg_replace($search, $replace, $subject) | 3.4 ms | 5.6 ms | 12.1 ms | 19.0 ms | 40.1 ms | 535 |
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.2 ms | 0.2 ms | 0.2 ms | 0.2 ms | 0.8 ms | 100 |
preg_replace('/^,*|,*$/', "", $string) | 6.8 ms | 6.9 ms | 6.8 ms | 6.9 ms | 27.4 ms | 3409 |
preg_replace('/^,*|,*$/m', "", $string) | 11.4 ms | 11.3 ms | 11.3 ms | 11.3 ms | 45.3 ms | 5637 |
preg_replace('/^,+|,+$/', "", $string) | 0.5 ms | 0.5 ms | 0.5 ms | 0.5 ms | 1.9 ms | 232 |
preg_replace('/^,+|,+$/m', "", $string) | 0.4 ms | 0.4 ms | 0.5 ms | 0.5 ms | 1.8 ms | 224 |
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …)) | 0.8 ms | 0.8 ms | 0.8 ms | 0.8 ms | 3.1 ms | 381 |
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.1 ms | 1.2 ms | 7.6 ms | 10.0 ms | 100 |
split(",", $string) | 1.2 ms | 1.9 ms | 40.4 ms | 43.5 ms | 436 |
preg_split("/,/", $string) | 1.6 ms | 1.9 ms | 11.4 ms | 14.8 ms | 149 |
preg_match_all('/[^,]+/', $string, $matches) | 2.1 ms | 3.5 ms | 18.5 ms | 24.1 ms | 242 |
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++) | 43.9 ms | 6044 |
for ($i = 0, $count = count($array); $i < $count; $i++) | 1.0 ms | 139 |
for ($i = count($array) - 1; $i >= 0; $i--) | 1.0 ms | 131 |
for ($i = count($array) - 1; $i >= 0; --$i) | 0.9 ms | 129 |
$i = count($array); while ($i--) | 0.7 ms | 100 |
My conclusion:
count() is horribly slow. Always precalculate it, if possible.
| Method | Summary | Index |
|---|---|---|
$array[0] | 33.5 ms | 100 |
$array['key'] | 34.4 ms | 103 |
My conclusion: I like associative arrays.
| Method | Summary | Index |
|---|---|---|
'contains no dollar signs' | 0.7 ms | 108 |
"contains no dollar signs" | 0.7 ms | 110 |
'$variables $are $not $replaced' | 0.6 ms | 100 |
"\$variables \$are \$not \$replaced" | 0.6 ms | 103 |
"$variables $are $replaced" | 7.5 ms | 1235 |
$variables . ' ' . $are . ' ' . $replaced | 9.1 ms | 1497 |
$variables . " " . $are . " " . $replaced | 8.8 ms | 1444 |
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