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
Bob Sutherton20,160 Points
What is the purpose of starting with a blank variable?
This is the second time this has been done during this project and the second time that I really didn't understand what was going on.
In the original products.php file we created an empty variable called $output.
$output = "";
Then appended strings to this empty nothingness. Why? Now we are doing it again here with the $list_view_html variable.
$list_view_html = "";
Why are we doing this?
Chris Shaw26,676 Points
This empty string is very important as without it our code wouldn't work resulting in an warning and a blank output, I'll explain why.
Our output is one giant string of concatenated HTML and whatever else, in php we simply can't concatenate a variable without a value specified as only certain data types allow for concatenation.
To solve this we give our variable, which in this case is
$output, an empty string so that the php compiler knows how to join the new string(s) later on in our code.
The easiest way to understand why it's like this would be to remove the empty string and replace it with
null, it will quickly become apparent as to what happens without an empty string set first.
Hope that helps.
Greg Kaleka39,021 Points
This is very useful if you're going to be manipulating the string / variable in a function. If you were to instead declare the variable in the function (i.e. when you actually have a value to put in), the problem is, you end up having a variable whose scope is only inside the function. I'm not sure if you've gotten to the point of using functions and/or learning about scope yet, but functions are powerful and scope is a pain if you don't handle things well :).
Trust that this is a good practice.