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

Raju Kolte
Raju Kolte
3,110 Points

Will try many ways but can't find right solution ? please help me

The "flavor.php" include file contains a function named get_flavor. That function receives no arguments, and it returns a piece of text as its return value. Call that function and assign the return value to a variable named $flavor.

output.php
<?php

echo "Hal's favorite flavor of ice cream is ____.";

?>

1 Answer

Maximillian Fox
PLUS
Maximillian Fox
Courses Plus Student 9,236 Points

Hi there,

What the question is asking is to get the data that is output from the get_flavor function within the flavor.php file, and store it to a variable. Let's break this down into three parts.

Part 1: Including the file storing the function. The question tells us that the function we want is stored in flavor.php. So before our code can access this function, we must include it in our current php file. In your output.php file, we can use the include, include_once, require, or require_once functions to add this file. My personal preference in this case is require, because it will cause an error if the file is not found, and stop the script from executing. Let's do it!

<?php
// This will ensure that flavor.php file is accessible to your script
require( 'flavor.php' );
?>

Part 2: Calling the flavor function In PHP, you can call any function by typing its name, and then opening and closing a set of normal brackets, So we know that the function is called 'get_flavor'. Now that we have included the correct file, we can call it like this:

<?php
// calls the get_flavor function but doesn't assign it to a variable
get_flavor();
?>

We don't include any arguments because in the question, it told us that get_flavor receives no arguments, so you just need to open and close the brackets.

Part 3: Assign the output of this function to a variable. Now that we have called the get_flavor function, we can assign it to a variable by simply declaring the variable name and an equals sign directly to the left of where we call the function.

<?php
// assigns the output of get_flavor to the flavor variable on the left.
$flavor = get_flavor();
?>

Now you've done that; you should have the correct piece of text within the variable $flavor. The next part will ask you to echo it to the screen, but this is the easy bit now you have it :)