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

Code Challenge: Reviewing PHP Basics (item #2 of 3) - "Enhancing a PHP Application"

I am stumped over this one: have tried SEVERAL variations of the code, and still "no enchilada". Can I get a hand on this one??? (This item is a review on functions)

3 Answers

If you show your code we'll be able to help.

well, do you just return the variable (ie., return $flavor;), in the body of the function?? Even though there are no arguments passed into the constructor?? Moreover, OUTSIDE of the function, do you THEN set the return variable ($flavor) equal to the function (get_flavor)? Without an explicit value??

the function was is probably written like this:

<?php function get_flavor(){ return "Cookie Dough"; } ?>

and when you want to call it to action you do this:

<?php get_flavor(); ?>

but in this challenge they want you to call the function get_flavor and store it's return value in a variable called flavor. Here's a hint:

<?php variable = function; ?>

the function was is probably written like this:

<?php function get_flavor(){ return "Cookie Dough"; } ?>

and when you want to call it to action you do this:

<?php get_flavor(); ?>

but in this challenge they want you to call the function get_flavor and store it's return value in a variable called flavor. Here's a hint:

<?php variable = function; ?>

Thanks Elliott, For some reason, somethings still not quite there....However, I did set my return value equal to the function as you illustrated, and that seemed to steer me in the right direction. Thanks for your time and effort.

could you display your code so I can have a better idea upon how to diagnose the problem.

Code challenge #2:

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.

My code:

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

var $flavor;

function get_flavor() { return $flavor }

get_flavor(flavor);

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

?>

Okay I copied your code and numbered the lines of code so I can reference to them easily. Your code:

1. <?php include("flavor.php");
2.
3. var $flavor;
4. 
5. function get_flavor() { return $flavor }
6. 
7. get_flavor(flavor);
8. 
9. echo "Randy's favorite flavor of ice cream is ____.";
10. 
11. ?>

Mistake on Line 3: you did "var = $flavor" but in PHP you don't need to add "var" before setting a variable like you do in Javascript.

Mistake on Line 5: when you included the "flavor.php" file on line 1 it already included the function, making the code on line 5 unnecessary.

Mistake on Line 7: what you put inside the parentheses in a function is taken as a argument, which is what you don't want to do.

Have you taken the "Build a Simple PHP Application" course yet?

Yes. As a matter of fact, I just finished that course, and moved on to this course (next in the series). Ok. I just figured it out. For some reason I'm "looking" past the obvious:

Mistake on Line 5: when you included the "flavor.php" file on line 1 it already included the function, making the code on line 5 unnecessary.

Thanks once again Elliott!

No problem! I'm glad you have it figured out.

i am having difficult time to solver this problem on task challenge 2 to 3 <?php include("flavor.php");

var $flavor;

function get_flavor(); { return $flavor }

$flavor=get_flavor(flavor);

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

?>

<?php
require('flavor.php');
$flavor = get_flavor();
echo "Randy's favorite flavor of ice cream is ____.";
?>

Hi, can Teamtreehouse please fix this exercise?