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 trialErin Braxton
666 PointsEcho info string inside of statment
I'm not sure what I did wrong here. Can someone take a look?
<?php
$name = "Mike";
if($name == 'Mike'){
$info = "Hi,I am Mike!";
}
echo $name;
?>
Erin Braxton
666 PointsI'm not able to pass this task, so I must be doing something wrong.
3 Answers
Erik McClintock
45,783 PointsErin,
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
16,310 PointsHi 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
8,378 PointsI 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!";
}
?>
S P
2,089 PointsS P
2,089 PointsThere is nothing wrong with your code. It runs fine.