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

Master Marketing
Master Marketing
1,200 Points

Help me understand the difference between the two blocks of code that switches the shirts' position the other way around

I have tried to understand the difference between the two over and over and can't seem to get it. How does

<?php

$list_view_html = get_list_view_html($product_id, $product) . $list_view_html;

?>

make the shirts's order change to orange, blue, grey, yellow?

How does changing the order of the concatenation affect this because as far as I understand the code, this block of code

<?php

foreach($products as $product_id => $product) { 
                            $position = $position + 1;
                            if ($total_products - $position < 4) {

?>

displays the last 4 shirts from yellow, grey, blue, orange so even if you concatenate it this way

<?php

$list_view_html = get_list_view_html($product_id, $product) . $list_view_html;

?>

the shirts would still display from yellow to orange. So what did I miss? What's the difference? I can't seem to understand it.

Any help is very much appreciated. Thank you!

Hi there,

Fixed markdown for the posted code. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.

While you had triple backticks, they were missing the php language specifier, and php code still needs to be wrapped by opening and closing tags to benefit from syntax highlighting.

<?php
    /* code */
?>

Cheers

2 Answers

Marileen Mennerich
Marileen Mennerich
1,161 Points

It helps if you just write it out :) The first option puts the result of each loop after the stuff you had before, so it'll be

yellow

yellow, grey

yellow, grey, blue

yellow, grey, blue, orange

Now, if you turn it around and put the new code in front of your old code, it becomes

yellow

grey, yellow

blue, grey, yellow

orange, blue, grey, yellow

In each line I bolded the new piece of code. The loop still works through the colors in the same order, that is true. But by concatinating them the other way around, the order reverses in the output. Does it become clearer now?

Master Marketing
Master Marketing
1,200 Points

Thank you for your help! I know it reverses the output but what I don't know is how. How does switching the two variables around make the output reverse?

Marileen Mennerich has a pretty good explanation

<?php

$list_view_html = get_list_view_html($product_id, $product) . $list_view_html;

For every iteration of your foreach loop, you're 'growing' the string inside the $list_view_html variable with the html for an additional shirt.

The html string of each new shirt can be placed at the front (prepended) or the back (appended) of the string inside $list_view_html.

You could do the same with alphabet.

<?php

$letters = array(
  'a',
  'b',
  'c',
  'd'
);

// Here we're going to loop through the letters and create a new string in the alphabet variable.
$alphabet = '';

// Append the new letter to the end of your alphabet string would give you all the letters in order:
foreach ($letters as $letter) {
  $alphabet = $alphabet . $letter; // would give you a, b, c, d
}
// You're saying put the letter 'c'  after 'ab'. Then put the letter 'd' after 'abc' etc...

// but prepend the new letter to the beginning of your alphabet string to reverse the order.
foreach ($letters as $letter) {
  $alphabet = $letter . $alphabet; // would give you d,c,b,a
}
// You're saying "but the letter 'b'  before 'a'. Then put the letter 'c' before 'ba'. Then put the letter 'd' before 'cba'

I'm sure somewhere there's room for a nice simple array_reverse.. Then you could skip all of this! :p

Marileen Mennerich
Marileen Mennerich
1,161 Points

I'm not sure I understand completely what exactly it is that's not quite clear to you. If it is the concatination operator (the period), it functions quite the way you loot at it. it concatinates the values in the exact order you write them. So "a . b" becomes ab, while "b . a" becomes ba.

I'm sorry if this is redundant again ^^