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

Conditional seems fine but isn't passing...

I have two conditionals to check if each student has a GPA of 4.0 or not. The preview has the correct output in it but the challenge doesn't seem to like my code. I have tried ==, ===, >= in my conditionals but none seem to work. I realise that I have repeated myself in this code and have not lived up to DRY programming practices but in this example it would be more involved to run a foreach loop. Any suggestions?

index.php
<?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;
}
?>

7 Answers

Hi, i checked and it does seem right to me, but it can be only bug try to write openig curly braces sticked to parenthesis without space, although this is not mistake I also noticed that some code challenges have strange syntax errors. This is example you should try.

index.php
<?php
if ($studentOneGPA == 4.0){  //Sticked curly brace to parenthesis
  echo $studentOneName . " made the Honor Role";
}
?>

If it doesnt work can you copy error that is giving to you

Here is my code which is working:

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

Yes that appears to have fixed the problem. It gave me a different error message when I brought the curly brace up onto the same line as the conditional and then I had to delete whitespace before each else clause. Thanks for your help!

I'm experiencing an issue with an error message reading - Make sure you add the else blocks

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

?>

Try formatting the whitespace in your if/else blocks like this: if (condition){ code } else { code }

That worked for me.

Sorry, my comment didn't really convey the line breaks. The if statement should have the opening curly brace immediately after the conditional in parentheses, then take a new line, and write the code for the block. Then take a new line, close the curly brace, type else, open a curly brace, take a new line and then write the code for the block. Then close the brace on a new line. Hope this helps.

Appreciate the help, stuck to your instructions but same error still reading. Is this a glitch within workspace or does php have issues in regards to whitespace and if/else statements?

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

?>

Thanks Daniel, so the issue was the whitespace between the if and else statements...