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 Enhancing a Simple PHP Application Refactoring the Codebase Duplicating SKU as an Array Element

Leigh Maher
Leigh Maher
21,830 Points

Unexpected result for Var Dump for $products

In this video Randy creates a variable called $removed and calls the array_shift function on the products array:

$removed = array_shift($products);

And then Var Dumps the $products variable. I don't get how this is showing the result of the $removed variable, as it's not calling that variable into the var dump?

If he's placing the array shift in the $removed variable, wouldn't you have to var dump that variable to see the result i.e.

var_dump($removed);

I'm confused as to how the $products gets changed in this scenario?

Thanks.

5 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

The removed variable is just a placeholder, somewhere to assign the method to, and that''s it's only function. It's like a paperclip holding a piece of paper together.

The var_dump isn't interested in the removed variable and all it does is display the contents of the variable that's passed in, which in this case is $products. :-)

Leigh Maher
Leigh Maher
21,830 Points

HI Jonathan. Thanks for the reply.

Yes, I agree that's what it should do. But if you comment out the line that sets up the removed variable, you will see a different result for the var dump of the products variable.

So, test it like this: var_dump($products);

and then like this: $removed = array_shift($products); var_dump($products);

You will get a different result for the var dump. In the first instance it shows the array with the associate key that we assigned to it in the array. In the 2nd instance it appears to remove the first product and reset the array keys.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,705 Points

But that's what array_shift() does. it removes the last element from an array() (or maybe the first) so once it's done, that element no longer exists in the array, and the others are pushed down to fill it's space. That's why the keys are reset.

It's not that the space is left free for another element, that key is filled with the next available element.

Leigh Maher
Leigh Maher
21,830 Points

Hi Jonathan. Sorry, I must not be explaining this very well. Just to clarify: I understand what the array_shift() does. What I'm not getting here is that we're locking the shift array up in a new variable called $removed, but then we're going back to the same var dump as before i.e. var_dump($products)

I would have thought that to access the result of the array shift function you would need to var dump the variable that has created the array_shift and not the original array that doesn't have the array_shift applied.

So, to see the result of the array_shift function I would have thought we'd have to do one of these two:

This: var_dump(array_shift($products));

or this: $removed = array_shift($products); var_dump($removed);

Do you see what I'm getting at here?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,705 Points

There is one think i thunk we both need to take into account. When we're looking at code in which variables are passed into a method, we read it from the middle and outwards. so the code looks at array_shift($products); first and then the removed array. So it removes the array element there and then and that's the result ytou see in the var dump which is the next action to perform.

<?php   var_dump(array_shift($products));   ?>

I think that's where we're seeing things differently. But we know it works because... check it out. :)

Leigh Maher
Leigh Maher
21,830 Points

In the example you've given above, yes it makes sense that it would show a shifted array. I understand that it works from the inside out, but that's not my point. Maybe this example will clarify what I mean. See the difference between these two:

<?php 
   $removed = array_shift($products);
     var_dump($products);
?>
<?php 
   array_shift($products);
     var_dump($products);
?>

In the 2nd example, I didn't put the array shift into a variable, so I would expect the dump to show the shifted array. In the first example, I've locked the shifted array up into a variable. I'm still not getting how we can access the shifted array without doing a var dump of the $removed variable.

Yes, I can see that what he's done does produce the result he's looking for, but I can't understand the logic.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,705 Points

Maybe you and I are at a loose end in terms of our understanding of the code lol

I don't think there's any specific purpose for "removed" other than being a way to organise the code. However my suspicion is that if you were to pass $removed into the variable then it would work the same was as passing $products

Maybe I can raise Randy Hoyt who can explain it better than me lol

Leigh Maher
Leigh Maher
21,830 Points

Thanks a lot, Jonathan.