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 trialPaul Kirsch
2,894 PointsI can't figure this assignment out
I'm stumped. I like "vanilla" Randy likes "cookie dough". I tried various combinations of: $flavor="vanilla" if $flavor="cookie dough" echo "Randy's favorite flavor is $flavor, also!"
thinking it would simply not echo. but I get an error message.
Paul Kirsch
2,894 Pointsthe question I believe was that if I didn't like the same flavor as the instructor, I should list my flavor and if it did not match it should not display (echo) the last sentence that the instructor likes the same flavor.
so: $flavor="vanilla" if $flavor="cookie dough" echo "Randy's favorite flavor is $flavor, also!"
Petros below suggests I put in a double equal "==" so the last line reads:
if ($flavor == "cookie dough") echo "Randy's favorite flavor is "$flavor", also!"
I am not sure why he put a period before and after the variable. if you have some thoughts let me know.
4 Answers
Petros Sordinas
16,181 PointsYes, if you want to concatenate a string with a variable or two strings together. That is what the "." does in PHP. For example, if $flavor="Vanilla", and the code is like this: echo "Randy's favourite flavour is $flavor, also!";
the output will be "Randy's favorite flavour is $flavor, also!"
If you write the code like this echo "Randy's favorite flavour is ".$flavor.", also!";
, PHP will output "Randy's favorite flavour is ", then the value of the $flavor variable and finally the ", also!" string.
Here is a link to the php manual for this: http://www.php.net/manual/en/language.operators.string.php - it has quite a few examples.
Petros Sordinas
16,181 PointsMake sure that when you assign a value to a variable you use a single '=' and when you compare values you use a double '==' sign.
The conditional as you wrote it in your post should be:
if ($flavor == "cookie dough") echo "Randy's favorite flavour is ".$flavor.", also!";
Paul Kirsch
2,894 PointsThanks Petros. I am not clear why you put a dot "." before and after the variable. Is that necessary?
Paul Kirsch
2,894 PointsThanks. excellent. Best, P.
Petros Sordinas
16,181 PointsAnytime! :)
Jerry Lopez
8,128 PointsJerry Lopez
8,128 PointsCan you provide a sample of what you are trying to code? Also are you just trying to output something?