Elseif vs else if

[LEFT]I saw this done a few times:

if ($) {}
else if ($$) {}
else if ($$$) {}

is it better to use elseif? And does it make a difference?
I thought else could only be used once


if ($) {}
elseif ($$) {}
elseif ($$$) {}

[/LEFT]

use switch to stop using so much if’s

agree

Suggesting switch over if/else without context does not work. Switch statements cannot handle complex case expressions. (Well I should say not to be used like that.) Only with context can such a recommendation be made.

There is no difference between “elseif” or “else if” just another way to write it. If statements allow multiple elseifs which can then be followed by else.


if () { ... }
else if () { ... }
else { ... }

I prefer using ‘else if’, but that’s just by convention and it tends to make the code look a little neater, for me.

But other than that there is no difference what so ever.

There is no performance difference for the two, nor any logical difference. It is just a code style difference, pick the one you prefer :slight_smile:

Thats kind of cool, less to worry about :slight_smile:

Same. ‘else if’ is the way it’s done in most languages, so I have this warming feeling inside of me that it’s the better way if at some point the PHP developers lose their mind and decide that elseif is bad. Not likely to happen, but I’m paranoid when it comes to my code.

This may be a bad way of doing things, i’ll let the experts shoot it down but i’ve done this a few times:


<?

			$a = 1;
			$b = 2;
			$c = 3;

			switch(true) {
				case ($a + $b == $c) :

				break;
				case ($a + $b != $c) :

				break;
				case (isset($_SESSION['test'])) :

				break;
			}

		?>

This may be a bad way of doing things

It is.

What benefits can you think of by doing it that way? Surey you can’t see the above as neater than:

<?php
$a=1;
$b=2;
$c=3;
if($a + $b == $c){
	//process
}else{
	//process
}
if(isset($_SESSION['test'])){
	//process
}
?>

I’ll quote my self

I prefer if statements…

Going back to the OP’s original question (or what I thought it was, rather than the recent derailment of switch vs if), the difference as I understand it is that else if is a nested construct whereas elseif (or Perl’s slightly more concise elsif) is not.

What I mean by that is, if we reformat the code slightly, an else if construct is effectively doing this:


if(x) {
   // do stuff
} else {
   if(y) {
      // do stuff
   } else {
      if(z) {
         // do stuff
      } else {
         // do stuff
      }
   }
}

It is nesting if statements. We just write it as:


if(x)
   // do stuff
else if(y)
   // do stuff
else if(z)
   // do stuff
else
   // do stuff

On the surface, it appears as though the above structure will, for reasons of local variable scope among other things, keep pushing onto a stack for each level in, whereas an elseif statement could simply evaluate conditions in turn and avoid that.

It may (depending on the language) all compile down to the same thing for all I know (my knowledge doesn’t extend that far), or it may not.

Just something people may have not considered.

Cheers,
D.

I prefer else if.

I also generally prefer multiple else ifs over a switch statement, unless it’s obvious I need to use a switch. The syntax just bugs me.

As logic_earth suggested earlier, there is a context for using switch, however switch is faster when executing than if else.

An if else statement must test each of the multiple conditions as it goes through - a switch statement will test the condition at the top, then jump directly to where the code is executed (if necessary).

Each statement has its place.