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 Separating Methods

jinhwa yoo
jinhwa yoo
10,042 Points

help me

<?php

class Render{

public static function listIngredients($ingredients)
{
  $output = "";  -----> why did you put?   

foreach ($ingredients as $ing){ $output.= $ing["amount"]." ". $ing["measure"]. " " .$ing["item"]; $output.="\n"; } } public static function displayRecipe($recipe) { $output=""; -----> why did you put?
$output .= $recipe ->getTitle() . " by " . $recipe -> getSource(); $output .="\n"; $output .=implode(", ",$recipe->getTags()); $output .="\n\n"; $output .=self::listingredients($recipe->getIngredients());------------> help me self????? $output .="\n"; $output .=implode("\n", $recipe->getInstruction()); $output .="\n"; $output .= $recipe->getYield();
} }

?>

2 Answers

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

I believe you are asking the reasoning behind the

$output = "";

instruction. Is that correct?

If so, what you are doing is initializing the $output variable to the empty string so that you can then concatenate each string you pull from the array in the loop to the string. If you were to initialize the $output variable within the string, it would only be available within the scope of the for statement, and you wouldn't be able to print the completed output string to the console after the for loop is completed running.

Does that make sense? (If not, let me know, and I'll try to answer it again in a different way. If that's not what you were asking, clarify your question, and I'll try again. :))

jinhwa yoo
jinhwa yoo
10,042 Points

public function __toString(){

$output ="You are calling a " . CLASS. "object with the title \""; $output .= $this ->getTitle(). "\""; $output .= "\nIt is stored in ". basename(FILE) . "at" .DIR . "."; $output .="\nThis display is from line ". LINE . "in method" . METHOD; return $output; } ???why put _tostring()???

You must define variable before you concatenate it