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

Rovie Dela Paz
Rovie Dela Paz
1,723 Points

Else if question

Hi, I'm trying to learn php and while watching the videos. I encountered this question.

Check if each student has a GPA of 4.0.

If the student has a GPA of 4.0, use the student name variable to replace NAME when displaying the following line:

NAME made the Honor Roll

ELSE If the students GPA is not equal to 4.0, within an else block, use the student name variable to replace NAME AND the student GPA variable to replace STUDENT GPA when displaying the following line:

NAME has a GPA of STUDENT GPA

and I've did this.

<?php
$studentOneName = 'Dave';
$studentOneGPA = 3.8;

$studentTwoName = 'Treasure';
$studentTwoGPA = 4.0;

//Place your code below this comment
if ($studentOneGPA ===  4.0){
  echo "$studentOneName made the Honor Roll";
}
else if($studentOneGPA <"4.0"){
  echo "$studentOneName has a GPA of $studentOneGPA \n";
}


if ($studentTwoGPA ===4.0){
  echo "$studentTwoName made the Honor Roll";
}
else if($studentTwoGPA <"4.0"){
  echo "$studentTwoName has a GPA of $studentTwoGPA \n";
}

?>

and it is saying that i need to create seperate if statements for each students. but for my understanding I already did create two if statements. can you guys please enlighten me up_ thank you. would really appreciate it.

3 Answers

Hey rovie, your code is fine you just need to remove the second if, cause the else will take care of handling it, the first if check the value of GPA is it equal to 4.0, if not (whatever the value could be, we don't care ) the else will display the msg.

NB : you can use the "==" operator, since they re only asking you to check if they re equal and not identical.

if ($studentOneGPA ==  4.0){
  echo "$studentOneName made the Honor Roll";
}
else{
  echo "$studentOneName has a GPA of $studentOneGPA \n";
}

Keep it up :)

Jesus Mendoza
Jesus Mendoza
23,289 Points

Hi Rovie, I have no clue about PHP but I saw that you were using 2 spaces between else and if, try with only 1 space and see if its fixed!

else if($studentOneGPA <"4.0")

You welcome, just for the record, your first code would work too, you just have to remove the " " around 4.0 in the second if, cause it is considering it as a string. but for this example you don't need a second if.