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 trialMark Lawson
6,148 PointsStruggling with else statement
$num = 100; - this falls in the range. I understand.
$num = 1; This does not meet both statements so else is displayed.
$num = 10000; This also does not meet both statements so why doesn't the Else statement display?
$num = 1; & $num = 10000; both fall outside the range. What am I missing? Why am I struggling to understand this?
Thanks
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! Well, first off the echo statements in the else are displayed, but maybe you're confused about which else statement is being displayed. So let's take a look at the code:
if($num >= 10){
if($num <= 1000) {
echo 'your number is within the range';
} else {
echo 'your number is greater than 1000, NOT within the range';
}
} else {
echo 'your number is less than 10, NOT within the range';
}
So let's say we set $num equal to 1. When we get to the first if statement, it will look to see if 1 is greater than or equal to 10. It is not and this comparison fails which leads us to the else statement that matches that if statement. Keep in mind here that the other if
and else
statements reside inside the first if
statement and will only be run if the first condition evaluates to true. So with $num equal to 1 it will echo out "your number is less than 10, NOT within the range".
Now let's say we set $num equal to 10000. This time when we look at the first if statement, it will pass the condition and evaluate to true. Ten thousand is greater than or equal to ten. Then we go to the next if statement within the first if
statement. At this point we will see if the number is less than or equal to 1000. It is not, and this comparison fails which will cause the else statement that matches up with that if
statement to execute. At this point, "your number is greater than 1000, NOT within the range" is echoed out.
Hope this clarifies things, but let me know if you have further questions!
Mark Lawson
6,148 PointsThank you Jennifer, your a star!
Mark Lawson
6,148 PointsMark Lawson
6,148 PointsI get it thank you - I think!.
With the $num = 100; The first if statement ran; it was true, and then moved onto the second if statement which was also true; so displayed 'your number is within the range'.
With the $num = 1; the if statement is false so goes to else statement. And displays 'your number is less than 10, NOT within the range'.
This was the tricky part for me. $num = 10000. The first else statement 'your number is greater than 1000, NOT within the range' is connected to the second if statement, and will display if the second if statement is false.
Am I correct in my logic? Oh, and I have a headache now OMG!
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherYes, Mark! You're correct. Here's the thing about
if
andelse
statements. Only one of those will ever be run. If theif
evaluates to true it is run, otherwise the else is run. Once it it run it stops evaluating to see if anything else matches. Period. So in the case of 10000 it passes the first condition and begins running the code inside that block. Now, that code could've been just about anything. But in this case, it happened to be anotherif
else
statement. So when it starts running that block it encounters the secondif
, but it fails that condition, so it moves to theelse
statement inside that block. Once that is completed it exits that block back to the originalif
statement that started it. But because there's no additional code other than theif
else
inside that block, then thatif
else
is also exited. Again, only theif
orelse
will be executed in one block. Not both. And we've already oneif
from the outer block and oneelse
from the inner block.