0

Hello all community again !

I only want to know what is faster and better (logical, not writing or tabbing) between:

IF...ELSE...IF...ELSE...
IF...ELSEIF...ELSEIF...ELSE...

For example:

$i=0;

if($i>5){
   echo '$i is greater than 5.';
}else{
    if($i>0){
        echo '$i is greater than 0 but less than 6.';
    }else{
        echo '$i is equal to 0.';
    }
}

OR

$i=0;

if($i>5){
   echo '$i is greater than 5.';
}elseif($i>0){
    echo '$i is greater than 0 but less than 6.';
}else{
    echo '$i is equal to 0.';
}

Thanks all !

Edited by P0lT10n: n/a

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by almostbob
0

http://php.net/manual/en/control-structures.switch.php

In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.

Edited by almostbob: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.