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 trialdarrell mayson
5,215 PointsKeep getting "undefined variable: measurements" error.
Hi,
Here's my script in it's entirety. I've been through it so many times I'm going cross-eyed. Error is at line: 54 but I cannot resolve it. Any help appreciated.
<?php
class Recipe { private $title; public $ingredients = array(); public $instructions = array(); public $yield; public $tag = array(); private $source = "Alena Holligan";
private $measurements = array(
"cup",
"tsp",
"tbsp",
"mg",
"g",
"kg",
"ml",
"l"
);
//print (echo) the recipe to screen
public function displayRecipe()
{
return $this->title . " by " . $this->source . "\n";
}
// set the recipe title attribute to camel case
public function setTitle($title)
{
$this->title = ucwords($title);
}
public function getTitle()
{
return $this->title;
}
// set the recipe source attribute to camel case
public function setSource($source)
{
$this->source = ucwords($source);
}
public function getSource()
{
return $this->source;
}
//set ingredients via associative array
public function addIngredient($item, $amount = null, $measure = null)
{
//check to ensure only valid measurements entered
if($measure != null && !in_array(strtolower($measure), $this->$measurements))
{
exit("Measure must be valid (metric) measurement. Use one of the following: " . implode(", ", $this->measurements) . "\n");
}
//check to ensure only int or float entered for amount
if($amount != null && !is_float($amount) && !is_int($amount))
{
exit("The amount must be a decimal. " . gettype($amount) . " entered. Please try again.\n");
}
$this->ingredients[] = array(
"item"=>ucwords($item),
"amount"=>$amount,
"measure"=>strtolower($measure)
);
}
}
//testing ... $recipe1 = new Recipe(); $recipe1->setSource("darrell mayson"); $recipe1->setTitle("my first recipe"); $recipe1->addIngredient("egg", "one", "doz"); echo $recipe1->displayRecipe();
$recipe2 = new Recipe(); $recipe2->setSource("anthony bordain"); $recipe2->setTitle("my second recipe");
echo $recipe2->getTitle() . " by " . $recipe2->getSource() . "\n";
?>
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! I sincerely hope this helps, but this is what I've found so far, and I'm no expert on php.
On line 47 you have this:
if($measure != null && !in_array(strtolower($measure), $this->$measurements))
I feel fairly certain this should be:
if($measure != null && !in_array(strtolower($measure), $this->measurements)) /* note the remove of the $ before the measurements */
I feel like that's why you're getting the unidentified variable error. Make that change and see if it helps!
darrell mayson
5,215 PointsMy hero! OM*G a '$'..really! lol Thank you sooooo much. Happy coding can resume..
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse Teacherdarrell mayson Oh good! Glad that fixed it