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 a Collection Filtering

sujal khatiwada
sujal khatiwada
Courses Plus Student 389 Points

Not displaying Breakfast recipes

this is my code for cookbook.php


$cookbook=new RecipeCollection("Treehouse Recipes");

$breakfast=new RecipeCollection("Favorite Breakfasts");

foreach($cookbook ->filterByTag("breakfast") as $recipe){

echo Render::listRecipes($breakfast->getRecipeTitles());

//echo Render::displayRecipe($belgian_waffles);


and code for recipecollection.php is


class RecipeCollection{
    private $title;
    private $recipes =array();

    public function __construct($title=null){

    public function setTitle($title)

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

    public function addRecipe($recipe){
        $this->recipes[]= $recipe;

    public function getRecipes()
        return $this->recipes;

    public function getRecipeTitles(){
        foreach($this->recipes as $recipe){
        return $titles;

    public function filterByTag($tag){

        foreach($this->recipes as $recipe){
            if(in_array(strtolower($tag), $recipe->getTags())){
            return $taggedRecipes;




but I cant see the output

Moderator edited: Added markdown so the code renders properly in the forums.

1 Answer

You have the return $taggedRecipes; inside the scope of the foreach loop in your recipecollection.php try moving it one parenthesis down and test again

A function can only return once, and your return wasn't even in the if statement(which would still be wrong), so it would just return on the first foreach loop and stop execution.