PHP PHP Arrays and Control Structures PHP Conditionals School's Out

Lee Howard
Lee Howard
6,620 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
MOD
Sean T. Unwin
Treehouse Moderator 28,648 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,620 Points

Definitely more clear, thank you!!