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

thanmai Deekshith
thanmai Deekshith
1,687 Points

Code Challenge Conditionals

Check if each student has a GPA of 4.0. If the student has a GPA of 4.0, use the student's name variable to display "NAME made the Honor Roll". If not, use the variable to display "NAME has a GPA of GPA". Would appreciate if someone could help me with this. my code:

<?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{
  echo "$studentOneName has a GPA of $studentOneGPA";
}
if ($studentTwoGPA >= 4.0){
  echo "$studentTwoName made the Honor Roll";
}
else{
  echo "$studentTwoName has a GPA of $studentTwoGPA";
}

?>

So what's the question?

formatted code

19 Answers

<?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 {
  echo $studentOneName . " has a GPA of " . $studentOneGPA;
}

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

?>
Roman Delcarmen
Roman Delcarmen
3,088 Points

Gracias esta es la respuesta correcta

Andres Ramirez
Andres Ramirez
18,094 Points

This was a fairly easy solution. I would have expected to declare a new variable combining both the students etc etc. I'm sure this code can be simplified!

Igor Pavlenko
Igor Pavlenko
12,925 Points

i like you formatting, did it this way. still works however your solution looks cleaner !!

$studentOneName = 'Dave';
$studentOneGPA = 3.8;

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

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

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

this is the worst challenge with the least amount of direction I have come across so far on Treehouse. Really bad. Please update this.

I agree David Soards ... The challenges are very unclear in what they are looking for

i agree

Adam Sims
Adam Sims
688 Points

Formatting on this is a real issue. PHP isnt fussy on whitespace, but this exercise is a different story.

This one's cleaner, shorter, concise and doesn't rely on type juggling:

<?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 {
  echo "$studentOneName has a GPA of $studentOneGPA";
}

if ($studentTwoGPA === 4.0) {
  echo "$studentTwoName made the Honor Roll";
} else {
  echo "$studentTwoName has a GPA of $studentTwoGPA";
}
?>
Zachary Scott
Zachary Scott
1,650 Points

** This worked well for me:**

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

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

//Check Student One:
if ($studentOneGPA  == 4.0) {
  echo $studentOneName . "made the Honor Roll";
} else {
   echo $studentOneName . " has a GPA of " . $studentOneGPA;
}

//Check Student Two:
if ($studentTwoGPA == 4.0) {
  echo $studentTwoName . " made the Honor Roll";
} else {
  echo $studentTwoName . " has a GPA of " . $studentTwoGPA;
}
?>
JEAN M CETOUTE
JEAN M CETOUTE
8,356 Points

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

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

Hi thanmai,

The challenge wants you to check if the gpa's are equal to 4.0

You're checking if they are greater than or equal to 4.0

Also, this challenge is picky about how you've formatted your code. The else statement needs to be on the same line as the closing brace for the if statement.

Like this

if ($studentOneGPA == 4.0){
  echo "$studentOneName made the Honor Roll";
} else {
thanmai Deekshith
thanmai Deekshith
1,687 Points

Hi Jason, Originally my code had == then I encountered the same issue, hence changed it to >=. I hope there are no other errors apart from >=?

Thanks & Regards, Thanmai

I also mentioned the issue with the else statement.

It needs to be on the same line as the curly brace ending the if.

You had it like this

}
else{

it needs to be like this

} else {

This is for the challenge only. It's requiring a very specific format.

Graham Patrick
Graham Patrick
8,294 Points

Not working for me but its outputting correctly

<?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 {
 echo "$studentOneName has a GPA of $studentOneGPA ";
}

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

outputting the right result! but not passing

Bummer! You need to check that $studentOneGPA is equal to 4.0

output is

Dave has a GPA of 3.8 Treasure made the Honor Roll

Graham Patrick
Graham Patrick
8,294 Points

sorted it !

<?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 {
 echo "$studentOneName has a GPA of $studentOneGPA ";
}

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

Remove the "" around the 4.0 in the if statement

I did the same code only with more spaces so that it looks better and it dint work. after I changed only the spaces and why spaces everything was working like it was the spaces.... why??

The challenges are usually looking for an exact match on the output. So you want to make sure that you follow any requirements in the instructions as well as maintaining any formatting that already exists in the starter code.

In other words, if the existing code is outputting everything mashed together on one line then your updated code should do the same.

In my opinion that statement does not have logic or sense. "Check if each student has a GPA of 4.0. If the student has a GPA of 4.0, use the student's name variable to display "NAME made the Honor Roll". If not, use the variable to display "NAME has a GPA of GPA". Would appreciate if someone could help me with this. my code:"

does not have logic or sense if de result is this " if ($studentOneGPA == 4.0) { echo $studentOneName . " made the Honor Roll"; } else { echo $studentOneName . " has a GPA of " . $studentOneGPA; }

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

Hi Ramon,

Can you clarify what you mean?

I think the statement is not clear enough to solve the exercise

if the result is this "if ($studentOneGPA == 4.0) { echo $studentOneName . " made the Honor Roll"; } else { echo $studentOneName . " has a GPA of " . $studentOneGPA; }

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

<?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 {
  echo "$studentOneName has a GPA of $studentOneGPA";
}

if ($studentTwoGPA == 4.0) {
  echo "$studentTwoName made the Honor roll"  ;
} else {
  echo "$studentTwoName has a GPA of $studentTwoGPA";
}
?>

still says im wrong any ideas?

it gave me trouble as well.

your "Roll" is spelt with a lowercase "r". It looks fine otherwise

<?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 {
echo $studentOneName.  " has a GPA of $studentOneGPA";
}
if ($studentTwoGPA== 4.0) {
echo $studentTwoName. " " . "made the Honor Roll";

} else {
echo $studentTwoName.  " has a GPA of $studentTwoGPA";
}

?>
Roman Delcarmen
Roman Delcarmen
3,088 Points

I have to keep studying concatenation

I have no idea what's going on here! :-/

Recep Onalan
Recep Onalan
15,541 Points

I made this and it worked

$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 {
  echo “$studentOneName “ . “has a GPA of $studentOneGPA” . “\n”;
}
if ($studentTwoGPA == 4.0) {
  echo “$studentTwoName “ . “made the Honor Roll”;
} else {
  echo “$studentTwoName “ . “has a GPA of $studentTwoGPA”;
}
?>
James Osborne
James Osborne
5,693 Points

this is my answer yet it returns with "You will need to create separate if statements for each student"

$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";
}


?>
Dan Maliano
Dan Maliano
12,142 Points

not else if. just else...