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 Build a Simple PHP Application Working With Functions Displaying Only Four Shirts

Bob Sutherton
Bob Sutherton
20,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?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Brock,

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.

Hello Chris So why this approach of concatenating function result to the blank variable has not been implemented when displaying 8 t-shirts in the original t-shirt.php page?

Greg Kaleka
Greg Kaleka
39,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.

Bob Sutherton
Bob Sutherton
20,160 Points

I just don't understand the logic for its original purpose. What is the point of ""? I guess it's the concatenation aspect of it that has me confused by putting the variable inside of itself over and over.

Greg Kaleka
Greg Kaleka
39,021 Points

In some languages you can declare a variable without giving it a value. Not in PHP. So if you want to declare it, you have to give it something like an empty string, or a 0 if it's an int.

hmm...I think what he is saying is why start out with $output = "", instead of $output = "li". I kind of wondered the same thing, since testing it out, both have the same output.

Greg Kaleka
Greg Kaleka
39,021 Points

Colby,

You don't want to declare it as $output="li" to start, because you want your formula to determine the entire output. If you change your implementation later, you don't want to have to track down what you put into the output before even starting the formula.

cool, i think i get it :)

kind of like a best practice, kind of thing?