What is the alternative to using multiple elseif nested loops in PHP
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
athen
b,if
cthen
b,if
dthen
a sometimes b), then stick with ifs. (As said here )
source to share