vendredi 28 août 2020

Which If statement is faster; comparison operators or in_array?

I'm creating several functions checking values between or in some interval. Since these functions will be called ten thousand of times, which one should I use for the following sample scenario?

Lets consider I have 1..N variables, I want to check whether the input is in N set or not.

Sample case;

Code numbers between 1 to 15 (also sometimes; numbers between 1 to 15 but not 8 etc.)

First option;

if ($num >= 1 && $num <= 15) {
    //..
}

Second option;

if (in_array($num, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], TRUE)) {
    //..
}

Aucun commentaire:

Enregistrer un commentaire