PHP Build a Basic PHP Website Listing and Sorting Inventory Items Review Basics

Cliff Jackson
Cliff Jackson
2,886 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

Spenser Hale
Spenser Hale
18,327 Points

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,886 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.

Spenser Hale
Spenser Hale
18,327 Points

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.