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 (Retired) PHP Conditionals & Loops Conditionals Challenge

Bruce Hayes
PLUS
Bruce Hayes
Courses Plus Student 658 Points

IF statement problem

No sure why this does not work.

index.php
<?php
$name = 'Mike';
if ( $name == 'Mike') {
  $greeting = 'Hi, I am Mike!';
}
<$php 
  <p>
  echo 'greeting';
  </p>
  $>
?>

2 Answers

Bruce, when they say echo they mean in part to use the $name variable rather than hard-coding the name:

<?php
$name = 'Mike';
if ( $name == 'Mike') {
  echo 'Hi, I am ' . $name . '!';
}
?>

The other is that they wanted the string echoed, not assigned to another variable.

Happy coding!

Austin Whipple
Austin Whipple
29,725 Points

Looks like you've over-complicated this a bit for this challenge. Rather than setting a variable of $greeting and then echoing it outside the if statement, they're just asking you to echo the string right inside the if statement. Like so:

<?php
 $name = 'Mike';
 if( $name == 'Mike' ) {
   echo 'Hi, I am Mike!';
 }
?>

All that said, you can achieve some of what you're after with your code block by correcting some syntax errors. First, you want to be sure your PHP tags are opened and closed in pairs and not nested within each other. Yours were nested. Also, be careful of how they're typed (question marks, not dollar signs).

Second, HTML within PHP blocks must be contained in a string to echo onto the page. So you'd probably want to add them to your $greeting variable (or you could include it in the echo line wrapping your greeting variable).

<?php
$name = 'Mike';
if ( $name == 'Mike') {
  $greeting = '<p>Hi, I am Mike!</p>';
}
?>
<?php  echo "$greeting"; ?>

That code stands a better chance of working on a page (though not this challenge).