View Issue Details

IDProjectCategoryView StatusLast Update
5742Composrcorepublic2024-07-22 21:22
ReporterPDStig Assigned ToGuest  
PrioritynormalSeverityfeature 
Status newResolutionopen 
Summary5742: Consider in_array when there are 3 or more string comparisons
Descriptionin_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.
TagsRoadmap: Over the horizon, Type: Performance
Attach Tags
Time estimation (hours)
Sponsorship open

Sponsor

Date Added Member Amount Sponsored

Activities

Chris Graham

2024-07-22 21:22

administrator   ~8877

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)

Add Note

View Status
Note
Upload Files
Maximum size: 32,768 KiB

Attach files by dragging & dropping, selecting or pasting them.
You are not logged in You are not logged in. This means you will not get any e-mail notifications. And if you reply, we will not know for sure you are the original poster of the issue.

Issue History

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