View Issue Details
ID | Project | Category | View Status | Date Submitted | Last Update |
---|---|---|---|---|---|
5742 | Composr | core | public | 2024-04-28 19:16 | 2024-07-22 21:22 |
Reporter | PDStig | Assigned To | Guest | ||
Priority | normal | Severity | feature | ||
Status | new | Resolution | open | ||
Summary | 5742: Consider in_array when there are 3 or more string comparisons | ||||
Description | in_array is more readable and possibly faster if comparing 3 or more strings with the same condition in an if guard. It can also be used for strict comparison of multiple strings (with in_array parameter 3 being set to true). Consider adding this to coding standards / code linter. | ||||
Tags | Roadmap: Over the horizon, Type: Performance | ||||
Attach Tags | |||||
Time estimation (hours) | |||||
Sponsorship open | |||||
|
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) |
Date Modified | Username | Field | Change |
---|---|---|---|
2024-04-28 19:16 | PDStig | New Issue | |
2024-04-28 19:16 | PDStig | Tag Attached: Roadmap: Over the horizon | |
2024-07-22 21:22 | Chris Graham | Tag Attached: Type: Performance | |
2024-07-22 21:22 | Chris Graham | Note Added: 0008877 |