Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.