<?php
$total = 10000;
$paragraph = 'this is a sentence. Crud! $$$$!';
$words = explode(' ', $paragraph);
$badWordHash = [
'$$$$' => true,
'@#$%' => true,
'crud' => true,
'fud' => true,
'fudd' => true,
'dud' => true
];
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
foreach ($words as $word) {
isset($badWordHash[strtolower($word)]);
}
}
echo "isset: ", (microtime(true) - $s), PHP_EOL;
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
foreach ($words as $word) {
array_key_exists(strtolower($word), $badWordHash);
}
}
echo "array_key_exists: ", (microtime(true) - $s), PHP_EOL;
preferences:
37.22 ms | 402 KiB | 5 Q