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 School's Out

Lee Howard
Lee Howard
6,621 Points

Can someone break down the schools out code?

I really don't understand why there is a no operator when checking the name? I know this creates condition to be true, but why? When the first name and last name is Alena Holligan, couldn't we of just used the == operator?

Also why on line .5 is there a new else, then a brand new if statement on line 6? couldn't we of just used elseif?

I understand how the switch works, but everything else before it seems to of really stumped me.

I'd really appreciate a breakdown of this.

Thank you!

<?php 

$firstName = 'Alena';
$lastName = 'Holligan';
$currentGrade = 9;
$finalAverage = .88;
$messageBody = '';

if (!$firstName || !$lastName) {
  echo 'Please enter a student name';  
} elseif ( $currentGrade < 9 || $currentGrade > 12 ) {
  echo 'This is only for high school students, please enter a grade between 9 - 12';
} else {
  if ($finalAverage < .70) {
    $messageBody = 'We look forward to seeing you at summer school, beginning July 1st!';
  } else {
       switch ($currentGrade) {  
         case 9:
            $messageBody = 'Congratulations on completing your freshman year in High School! We’ll see you on September 1st for the start of your sophomore year!';
         break;
           case 10:
            $messageBody = 'Congratulations on completing your sophomore year in High School! We’ll see you on September 1st for the start of your junior year!';
         break;
           case 11:
            $messageBody = 'Congratulations on completing your junior year in High School! We’ll see you on September 1st for the start of your senior year!';
         break;
           case 12:
            $messageBody = 'Congratulations! You’ve graduated High School! Don’t forget to come back and visit.';
         break;
       default:
         $messageBody = 'Error: Grade level is not 9 - 12';
         break;
       }
 }
  echo "Dear $firstName $lastName\n";
  echo $messageBody;
}

2 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

I really don't understand why there is a no operator when checking the name? I know this creates condition to be true, but why? When the first name and last name is Alena Holligan, couldn't we of just used the == operator?

if (!$firstName || !$lastName) { is saying, "When $firstName OR $lastName is empty Then display a warning message". The operator is the exclamation mark ('!') before each variable.

Also why on line .5 is there a new else, then a brand new if statement on line 6? couldn't we of just used elseif?

The else here is saying, "When the first if is false And the elseif is false Then do this part". We don't need to use an elseif because we don't care about anything else right now except that the other two statements are false.

The nested if here is saying, "We've made it this far, but now we have have another choice to make based on whether the $finalAverage is less than .70.

Lee Howard
Lee Howard
6,621 Points

Definitely more clear, thank you!!