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 trial

PHP PHP Basics (Retired) PHP Conditionals & Loops If / ElseIf Conditionals

difference 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
Oğulcan Girginc
24,848 Points

Hi dariorancatore

Hope 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.
?>

The "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.

So 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
Oğulcan Girginc
24,848 Points

Yes. Also, you wouldn't use 4 ifs in a row. It should be if(), elseif(), elseif() and finally else().