#5742 - Consider in_array when there are 3 or more string comparisons This is a spacer post for a website comment topic. The content this topic relates to: #5742 - Consider in_array when there are 3 or more string comparisons By Guest posted 22nd Jul 2024, 5:22 PM Do not fill this field in. Hmm, I'm surprised but it is faster... <?php define('ITERATIONS', 10000000); $x = 'hello'; $a = microtime(true); for ($i = 0; $i < ITERATIONS; $i++) { if ($x == 'a' || $x == 'b' || $x == 'c') { } } $b = microtime(true); var_dump($b - $a); $a = microtime(true); for ($i = 0; $i < ITERATIONS; $i++) { if (in_array($x, ['a', 'b', 'c'])) { } } $b = microtime(true); var_dump($b - $a); float(0.28515386581421) float(0.10288000106812) 0 guests and 0 members have recently viewed this. Sort: Relevance Newest first Oldest first Rating Popularity
<?php
define('ITERATIONS', 10000000);
$x = 'hello';
$a = microtime(true);
for ($i = 0; $i < ITERATIONS; $i++) {
if ($x == 'a' || $x == 'b' || $x == 'c') {
}
}
$b = microtime(true);
var_dump($b - $a);
$a = microtime(true);
for ($i = 0; $i < ITERATIONS; $i++) {
if (in_array($x, ['a', 'b', 'c'])) {
}
}
$b = microtime(true);
var_dump($b - $a);
float(0.28515386581421)
float(0.10288000106812)