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 Basics Daily Exercise Program Conditionals

What's wrong with my code?

Here is the question. "Check if each student has a GPA of 4.0. If the student has a GPA of 4.0, use the students name variable to display "NAME made the Honor Role". If not, use the variable to display "NAME has a GPA of GPA"."

and my code is as follows:

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

$studentTwoName = 'Treasure';
$studentTwoGPA = 4.0;
if($studentOneGPA==4.0){
  echo "$studentOneName made the Honor Role.";
}
else{
  echo "$studentOneName has a GPA of $studentOneGPA.";
}
if($studentTwoGPA==4.0){
  echo "$studentTwoName made the Honor Role.";
}
else{
  echo "$studentTwoName has a GPA of $studentTwoGPA.";
}

//Place your code below this comment
?>

What's wrong with my code?

That is exactly what I had and it said my code was wrong too. I think the PHP Basic challenges are broken. Just about each one I do fails. I was just doing one about setting today's date and I tried every combination possible to echo and concatenate a string but I can't get it to work.

4 Answers

Patrick Masters
Patrick Masters
15,776 Points

Wow - that's exciting - never knew that (it's the little things) :)

But here's how to make it work, I actually took the quiz myself, first error mentioned else tags - apparently they need to be on the same line as the ending bracket

after that the next error mentions "using the variables" - I tried with your way (which is technically correct) but doesn't validate, this does:

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

if ($studentTwoGPA == 4.0){
  echo $studentTwoName. " made the Honor Role"; 
} else {
  echo $studentTwoName. " has a GPA of ". $studentTwoGPA; 
}
Patrick Masters
Patrick Masters
15,776 Points

Hey!

Since this is php, echo will.. well echo out whatever is in the quotes exactly! So for your first if statement it will literally say "$studentOneName made the Honor Role." on the screen (or wherever you're echoing too).

To use the variable, you want to form your echo like this:

echo $studentOneName . " made the Honor Role.";

for the next part same thing:

 echo $studentTwoName . " has a GPA of " . $studentTwoGPA . ".";

In php we use the dot operator to concatenate strings

I thought if you used double quotes echo will put the actual value of your variable and if you used a single quote echo would literally put in the variable name. So in the example echo "$studentOneName made the Honor Role." would result in Dave made the Honor Role. and echo '$studentOneName made the Honor Role.' would result as $studentOneName made the Honor Role.

Thank you very much. I really appreaciate it!

You are right BHA Team, Double quotes, as explain by Alena will expand the variable in double quotes.