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 Basic PHP Website (2018) Listing and Sorting Inventory Items Review Basics

Cliff Jackson
Cliff Jackson
2,887 Points

Function problem?

I cannot assign the variable $flavor to the get_flavor function. A long time back in the PHP course functions where covered but can't find a way on this one.??

output.php
<?php
include "flavor.php";
echo "Hal's favorite flavor of ice cream is ____.";
return get_flavor() {
   $flavor = get_flavor();
}
?>

2 Answers

Hello Cliff Jackson

The code you provided is appears to be a mix of defining a new function and calling a function. You have the correct answer there, just it is wrapped in some incorrect code.

How a function is defined: *(which you do not need to do since you are including it) *

<?php
function get_flavor() {
    return 'vanilla';
}

How a function is called:

<?php
get_flavor();

How to assign a variable from the return of a function:

<?php
$flavor = get_flavor();

So putting it all back together:

<?php
// Adds the flavor.php file to application, which in that file the function get_flavor() is defined.
include "flavor.php";
// Calls the get_flavor function and assigns its returned value to variable $flavor
$flavor = get_flavor();
// Takes the variable $flavor and prints it in the string
echo "Hal's favorite flavor of ice cream is $flavor.";
Cliff Jackson
Cliff Jackson
2,887 Points

I think one of the big problems i had was the order the code was put on the page which in the challenge(Happens a lot) there was no mention of having it in a certain order.

I agree that sometimes instructions can be out of order from how you need to implement, however working in this industry that will come up a lot, and it would be best to be familiar with understanding how you will need to procedural write the code.

I hope you were able to pass the challenge now. I would appreciate you selecting my answer as best answer if it helped you solve your problem.