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 Build a Simple PHP Application Creating the Menu and Footer Variables and Conditionals

I 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.

Can you provide a sample of what you are trying to code? Also are you just trying to output something?

the 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:

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
Petros Sordinas
16,181 Points

Yes, 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
Petros Sordinas
16,181 Points

Make 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!";

Thanks Petros. I am not clear why you put a dot "." before and after the variable. Is that necessary?

Thanks. excellent. Best, P.