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 Enhancing a Simple PHP Application Integrating Validation Errors Reviewing PHP Basics

How to call a predefined function

I am not understanding where I'm going wrong. I thought that since favor.php has the function get_flavor that I should be able to just call it. I tried putting the variable outside and inside the function call as well.

output.php
<?php
include("flavor.php");

$flavor = "";
get_flavor() {
  return $flavor;
}
echo "Randy's favorite flavor of ice cream is ____.";

?>

3 Answers

kevin jordan
kevin jordan
11,353 Points

Hey Michael - A couple of things to take into consideration :

**If get_flavor() is defined in "flavor.php" then you are kinda redefining it by declaring

get_flavor(){
return $flavor;
}

Note : in order to actually declare a function, you need the syntax :

function get_flavor(){
return $flavor;
}

**In order for a $flavor value to be echoed to the screen you need to instantiate it with a call in the echo statement. Something like this

echo "Randy's favorite flavor of ice cream is " . get_flavor();

**Right now you set $flavor = " " (an empty string), so when you do call get_flavor() the value returned will be an empty string.

So if get_flavor is defined in "flavor.php" you can call it simply with the line:

$flavor = get_flavor();

Then you can echo it to the screen with concatenation:

echo "Randy's favorite flavor of ice cream is " . $flavor;

Hope this helps a bit !

kj

daviem
daviem
21,118 Points

Hi Michael, below is some code with comments that I hope will help

<?php include('flavor.php'); // include the external flavor.php file into your script task1 of 3

$flavor = get_flavor(); // call the get_flavor() function from flavor.php and assign its return value to var $flavor task2

echo "Randy's favorite flavor of ice cream is ".$flavor."."; // concatenate the $flavor to the output string. task3 of3

?>

so you include the external file (flavor.php). This file has a function that we are now able to call, the return walue from the function (which will the randy's favourite ice-cream) will get saved into the $flavor variable, then it can be echo'd to screen using concatenation.

Hope this helps, good luck,

Kevin and David. Thank you for the replies. You both nailed what the problem was. I just wish I could both give you a best answer.

daviem
daviem
21,118 Points

Nice of you to say so Michael, and happy to help, Kev responded first, so that's cool to best answer,

All the best with your studies !

kevin jordan
kevin jordan
11,353 Points

No prob bud - glad you are on your way !! -kj