php – How to optimize the IF condition with many conditions and same output

Question

Sure

You could use in_array for this type of check, something like this

if (in_array($string, ['apple', 'banana', 'kiwi'])) { // keep adding as many as you need
    $Detail = false;
    $bShowNew = true;
}

If your array gets to big you can create a variable that will contain that array

$allowed = [
    'apple',
    'banana',
    'kiwi'
    // etc... 
];

if (in_array($string, $allowed)) { // keep adding as many as you need
    $Detail = false;
    $bShowNew = true;
}

This will do exactly the same but is a bit cleaner and easier to read

0
Sahasrar 1 month 2021-08-11T02:40:37-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse