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

Bára Paulátová
Bára Paulátová
3,583 Points

Changed the code so it always address the user by first and last name.

Hi,

I've tried to change the code so if the user fills in first and last name, but is not 9-12 grade, he will get this message: "Dear Alena Holligan, This is only for high school students. Please enter a grade between 9 and 12.

Do you see any trouble with the if-else nested conditionals? Or is there better way to do that?

$firstName ='Alena';
$lastName = 'Holligan';
$currentGrade = 11;
$finalAverage = 0.6;
$messageBody =' ';

if (!$firstName || !$lastName) {
  echo 'Please enter a student name';
} else {
  echo ('Dear $firstName $lastName, \n');
  } if ($currentGrade < 9 || $currentGrade > 12) {
  echo 'This is only for high school students. Please enter a grade between 9 and 12';
} else {
    if ($finalAverage < .70) {
      echo '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.';
      }
    }
  echo ($messageBody);
}
Bára Paulátová
Bára Paulátová
3,583 Points

Ups - and how do I insert the code so it doesnt get this messy ... sorry about that!

2 Answers

Antonio De Rose
Antonio De Rose
20,885 Points

when you are in the writing window, if you see Markdown Cheatsheet, that will help you to mention, how could you paste code, in a nicer and easier way to present your coding

3 backticks followed by programming language then paste coding ending again with 3 backticks

hint - backtick is left to digit number 1 in the keypad

example

"```php"

paste code here

"```"

your case, please do not use double quotes, if you did, it will not work the reason I put double quotes, is to show the backtick

Bára Paulátová
Bára Paulátová
3,583 Points

Thanks, Antonio! That worked. :) So I edited my original text.

Antonio De Rose
Antonio De Rose
20,885 Points

you missed the <?php

<?php
$firstName ='Alena';
$lastName = 'Holligan';
$currentGrade = 11;
$finalAverage = 0.6;
$messageBody =' ';

if (!$firstName || !$lastName) {
  echo 'Please enter a student name';
} else {
  echo ('Dear $firstName $lastName, \n');
  } if ($currentGrade < 9 || $currentGrade > 12) {
  echo 'This is only for high school students. Please enter a grade between 9 and 12';
} else {
    if ($finalAverage < .70) {
      echo '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.';
      }
    }
  echo ($messageBody);
}
Bára Paulátová
Bára Paulátová
3,583 Points

Right! I was wondering why my is not blasting with colours :) Thanks again!.