Welcome to the Treehouse Community
Looking to learn something new?
Raju Kolte3,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.
<?php echo "Hal's favorite flavor of ice cream is ____."; ?>
Maximillian FoxCourses Plus Student 9,236 Points
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 :)