Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdariorancatore
6,845 Pointsdifference else/elseif
Hi all, I did not understand well the difference between else/else if. Can someone help me? Thanks in advance!
3 Answers
Oğulcan Girginc
24,848 PointsHope this will make it more clear for you.
if
<?php
if(condition){
//Complies with the conditions, do it.
}
?>
else
<?php
if(condition){
//Complies with the conditions, do it.
}else{
//DON'T comply with the conditions, do it.
?>
elseif and else
<?php
if(condition){
//if this first one complies with the conditions, do it.
}elseif{
//if this second one complies with the conditions, do it.
}else{
//DON'T comply with the conditions, do it.
?>
Dario Morbidi
33,072 PointsThe "else" statement tells the program to execute a group of instructions when the "if" condition is evaluated to false; the "elseif" statement tells the program to execute a new "if" statement, when the original if condition is false.
dariorancatore
6,845 PointsSo the main difference is that while else is just a simple solution if the if is false, elseif is a new if statement?
Oğulcan Girginc
24,848 PointsYes. Also, you wouldn't use 4 ifs in a row. It should be if(), elseif(), elseif() and finally else().