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 Arrays and Control Structures PHP Conditionals Nested Conditional Statements

Mark Lawson
Mark Lawson
6,148 Points

$num = 10000

I am struggling to understand part of this video. When Alena changed the $num to 1, the else statement displayed. However, when she changed the $num to 10000 nothing displayed. Why didn't the else statement display? !0000 fell outside the range.

Thanks

2 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

The way the conditionals are written, the ELSE logic that echoes 'your number is NOT within the range' is tied to (dependent on) only the first IF statement ($num >= 10). Indentation is especially helpful with nesting, as it helps clarify which else blocks belong to which if blocks. Perhaps this might help clarify the flow of logic -

<?php
$num = 10000;

if ($num >= 10) {
    if ($num <= 1000) {
        echo 'your number is within the range';
    }
    else {
        echo 'your number was greater than or equal 10, but not less than or equal to 1000';  
    }
} 
else {
    echo ' your number is NOT within the range';
}

?>

In this case, rather than display nothing, a value of 10000 will be triggered.

If you have any more questions let me know and I can try to explain more.

*EDITED to make indentation more explicit.

Mark Lawson
Mark Lawson
6,148 Points

Hi Benjamin,

Not getting this at all. Totally struggling to make sense of this.

Thanks

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Mark,

Alena explains this starting at 2:06 in the video. The short answer is that there isn't an else statement for the second conditional. So, 10000 meets the first condition, but not the second condition, and there isn't any code to tell it what to do in that situation. But have a quick review where she explains it in more detail than that. :)

Keep Coding! :dizzy: