Member Avatar for P0lT10n

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 !

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.