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

Erin Braxton
Erin Braxton
666 Points

Echo info string inside of statment

I'm not sure what I did wrong here. Can someone take a look?

index.php
<?php
$name = "Mike";

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

There is nothing wrong with your code. It runs fine.

Erin Braxton
Erin Braxton
666 Points

I'm not able to pass this task, so I must be doing something wrong.

3 Answers

Erik McClintock
Erik McClintock
45,783 Points

Erin,

The challenge wants your echo statement to be inside of your if conditional. To have your echo statement be of more use, however, we'll want to utilize the value stored in the variable $name that we created, rather than echoing a hard-coded value.

You want:

<?php

$name = "Mike";

if( $name == "Mike" ) {
  echo "Hi, I am " . $name . "!";
}

?>

Here, we create a variable called $name and store the value "Mike" inside of it. We then write a conditional if statement that checks to see if the value stored in our $name variable is equal to "Mike", and if so, we want to echo out a statement to the screen that prints the value stored in that variable.

It looks like you were trying to do something similar in your code, but you chose to echo the wrong variable. In your case, you're creating a new variable called $info inside your if conditional, in which you are storing the "Hi, I am..." string, so you would then want to echo $info rather than $name. However, in this particular instance, the challenge will not allow that code to pass. It seem it is looking for a direct echo statement, like above.

Hope this helps to clarify some things!

Erik

Josh Cummings
Josh Cummings
16,310 Points

Hi Erin,

What is it that you are trying to achieve here?

As written, your code says that if the variable $name equals Mike, save the string "Hi, I am Mike!" to a variable $info.

You are then echoing out the name, which in this case is Mike.

If you are trying to echo the phrase only if $name equals Mike, you can move the phrase to the if statement like so:

<?php

$name = "Mike";

if($name == 'Mike'){
  echo "Hi, I am Mike!";
}

?>

Hope this helps.

Abe Layee
Abe Layee
8,378 Points

I am not a PHP developer but it should be like this. For the last part, the challenge is only asking you to pass the statement to the echo method().

<?php
$name = "Mike";

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