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 Object-Oriented PHP Basics Building the Recipe Associative Arrays

Zeljko Porobija
Zeljko Porobija
11,491 Points

My console shows no errors

Although I put "one" and "doz" as it had been done in the video, no errors were showed in the console. Did I do something wrong or it's just the console? Here's my code:

Zeljko Porobija
Zeljko Porobija
11,491 Points
recipes.php
<?php

class Recipe 
{
  private $title;
  public $ingredients = array();
  public $instructions = array();
  public $yield;
  public $tag = array();
  public $source = "ลฝeljko Porobija";

  private $measurements = array(
   "tsp",
    "tbsp",
    "cup",
    "oz",
    "lb",
    "fl oz",
    "pint",
    "quart",
    "gallon"
  );

  public function setTitle($title) 
  {
    $this->title = ucwords($title);
  }

  public function getTitle()
  {
   return $this->title;    
  }

  public function addIngredient ($item, $amount = null, $measure = null)
  {
    if ($amount != null && is_float($amount) && !is_int($amount)) {
     exit("The amount must be a float: " .gettype($amount). " $amount given"); 
    }
    if ($measure != null && in_array(strtolower($measure), $this->measurements)) {
     exit("Please, enter a valid measurement: " .implode(", ", $this->measurements)); 
    }
     $this->ingredients[] = array(
      "item" => ucwords($item),
      "amount" => $amount,
      "measure" => strtolower($measure)    
    );
  }


  public function displayRecipe()
  {
    return $this->title . " by " .$this->source;
  }
}

$recipe1 = new Recipe();
$recipe1->source = "Grandma Holligan";
$recipe1->setTitle ("my first recipe");
$recipe1->addIngredient("egg", "one", "doz");

$recipe2 = new Recipe();
$recipe2->source = "Bettie Crocker";
$recipe2->setTitle("My Second Recipe");

echo $recipe1->getTitle();
echo $recipe1->displayRecipe();
echo $recipe2->displayRecipe();

2 Answers

Simon Coates
Simon Coates
28,694 Points

I'm not entirely sure what you're trying to do and haven't seen the video, but at a glance your conditions are wrong. if you want a float, you probably want your error condition to be ($amount == null || !is_float($amount)). The measure condition also needs to be inverted, i think. ($measure == null || !in_array(strtolower($measure), $this->measurements))

Simon Coates
Simon Coates
28,694 Points

if you want a little more variety in terms of what your type can be, you might want to us is_numeric. (this confirms that a value is a float, int or valid numeric string)

Zeljko Porobija
Zeljko Porobija
11,491 Points

Yep, I missed an ! before is_float, it seems that it was the problem.