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 Adding Logic to Your Programs

ceriannejackson
ceriannejackson
23,759 Points

Not sure what's wrong with my code

I keep getting an error on the code challenge for this. It keeps telling me not to forget the else blocks. Can someone help me understand what is wrong my code please?

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

12 Answers

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey ceriannejackson,

I threw your code into the code challenge and formatted it out and it passed.

You had it correct, so try refreshing the page and throwing your code back in.

Also, here's the code as it passed on this end to check against in the event it doesn't pass again:

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

?>

Good luck with the course!

Daniel Gauthier
Daniel Gauthier
15,000 Points

Also going to pass this along, so it's easier for you to post code in the future:

How to Post Code on the Forums

There are two ways to share your code on the forums here, excluding using external tools from outside of Treehouse.

Method One

The first method is to use a series of three ` (backticks, located at the top left of the keyboard) without any spaces on one line, paste all of your code starting on the second line, then closing the code block with a second series of three backticks. Take a peek at the link for the "Markdown Cheatsheet" located above the "Post Answer" button anytime you're about to post a comment or answer. Using this method, the code will look like this:

```css
(code here)
```

Method 2

The second method is a little more convoluted, but it lets us look at your entire project and make our own copy to try fixing issues. I'll order the steps since it can be a little confusing the first time around:

  1. Open the workspace you're having trouble with.

  2. On the menu bar located to the left of the "Preview Workspace" button is the "Snapshot Workspace" (camera icon), click that.

  3. A popout will appear with another button that says "Take Snapshot", click that.

  4. It should create a new snapshot, which will appear beneath the "Take Snapshot" button in the popout. Click the snapshot you want to share.

  5. The snapshot should open a new browser tab. Highlight the url in the snapshot's browser tab (it should look like this: https://w.trhou.se/duckyj79b1i0 ).

  6. Create your forum post and paste the snapshot's url into the post. Other Treehouse users will be able to open the snapshot, then 'fork' a new copy to check out your code.

Keep in mind that you can only ever have five snapshots, but you can delete them by hovering over the snapshot located below the "Take Snapshot" button in the popout and clicking the trash bin icon.

Good luck!

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

Fixed some issues with the code challenge, original code should work now :)

Moutasem Raheem
Moutasem Raheem
2,802 Points

i try many times it's not working !

Ryan Lucas
Ryan Lucas
6,946 Points

// Hi, I have just tried to do this challenge and it doesn't work.

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

// I believe the above code should pass ? In the preview it looks fine.

ceriannejackson
ceriannejackson
23,759 Points

Thanks. I have typed that same code in loads of times on different days and it wouldn't work. I wonder why it works when I copy and paste it in!

Thanks for the code pasting tips too

Will Gorham
Will Gorham
37,186 Points

To follow up on this - I was having the same problem. My code seemed fine but it wouldn't pass, saying I was missing the 'else' block. It turns out the challenge requires the 'else' to be inline with the 'if' closing curly brace.

For example:

<?php

// This does NOT pass
if ($studentOneGPA == 4.0) { 
    echo "$studentOneName made the Honor Role";
}
else { // NOT in line with closing curly brace
    echo "$studentOneName has a GPA of $studentOneGPA";
}

// This DOES pass
if ($studentOneGPA == 4.0) { 
    echo "$studentOneName made the Honor Role";
} else { // In line with closing curly brace
    echo "$studentOneName has a GPA of $studentOneGPA";
}

I've been having this same problem!

Mario Nardi
Mario Nardi
19,875 Points

I have that same problem, this is 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 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";
    }

?>

And this is the problem feedback: Bummer! You need to check that $studentOneGPA is equal to 4.0

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Mario,

This is a similar problem to what Will Gorham was describing in this thread.

The problem is in the placement of the brackets in your if blocks.

You're writing them like this:

<?php

if($studentOneGPA == 4.0)
{   // <--- Problem One
  echo "$studentOneName made the Honor Role";
} else
    {   // <--- Problem Two
     echo "$studentOneName has a GPA of $studentOneGPA";
    }

?>

& the parser doesn't like it :D

Write them out like this and your code passes:

<?php

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

?>

Notice the placement of the opening braces. You want them on the same line as the preceding statement in order to pass the challenge. It's finicky, but at least you know you had the right code :D

Good luck!

Mario Nardi
Mario Nardi
19,875 Points

I tried that already, not working. It outputs: Bummer! Fail

Mario Nardi
Mario Nardi
19,875 Points

Why is my code all in green, I˙ve put it in PHP tags?

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Mario,

When you post code, don't forget to write the name of the language the code is written in after the third backtick, like this:

```php

<code here>

```

Otherwise, your entire code block will be displayed in green since the parser won't know what language the code is written in and subsequently won't know which color codes to use.

Here is your code with the name included after the third backtick:

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

?>

& here it is without the name:

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

?>

Good luck with the course!

I've tried every single solution given here, it still doesn't work for me. I even tried it in different browsers, just in case that the parser doesn't work on the one I'm using. It doesn't help. It gives the error message saying "Bummer! fail". I'm stuck since it doesn't really specify anything about why the error is displayed. Any suggestions?

Mario Nardi
Mario Nardi
19,875 Points

Have you tried deleting your cache in browser? Can you paste your code here?

Hello Mario, now I've tried once more after clearing cache, deleting browser history and all, but it still doesn't work. My code below:

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

It gives the error: "Bummer! fail"

Mario Nardi
Mario Nardi
19,875 Points

I think it˙s not problem in your code. I tried with my code that passed before, and it fails this time, even tried with this 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 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"; }
?>

But, when I open browser Console, I get this log:

Application Error: There was a problem getting data for the application you requested. The application may not be valid, or there may be a temporary glitch. Please try again later

Empty string passed to getElementById().application_vendor-8a23e8dce7b84386fe2b962820cd19c9.js:132:9827
Empty string passed to getElementById().application_vendor-8a23e8dce7b84386fe2b962820cd19c9.js:132:9811
Empty string passed to getElementById().application_vendor-8a23e8dce7b84386fe2b962820cd19c9.js:132:9811

Could be problem on their end, someone from their staff should get back regarding this issue.

Brilliant, I've just tried it, it works now. Thank you, Alena :)