What is the alternative to using multiple elseif nested loops in PHP

What's the best alternative to using multiple (23 in my case) if-elseif

nested loops in PHP?

+3


source to share


2 answers


If your task is performance critical , the operators if

use validation followed by the next section, where, like in switch

, the value is loaded first, compared and iterated through the table of values ​​to find a match, which is faster in most cases.

For readability, I think it's switch

better if you have more than two conditions.

But first of all, it all depends on the circumstances of use.



If you have doubts about the choice :

  • Choose switches

    when you have an easy-to-read expression that will generate multiple results that must then be executed based on logic on.

  • If your expressions are not related to each other, are only cast to boolean conditions, or become complex / related (e.g. if

    a then

    b, if

    c then

    b, if

    d then

    a sometimes b), then stick with ifs. (As said here )

+4


source


The switch statement will do this. http://php.net/manual/en/control-structures.switch.php



0


source







All Articles