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 trialei2
2,618 PointsWhere did $key and $val come from?
A question, and a comment, really.
In this code:
while ((list($key, $val) = each($learn)) && $count++ < 2) { echo "$key => $val\n"; }
There is $key and $val within list(). Where did did $key and $val come from? Aren't they variables? Why are they not declared first?
As I understand, (or not, lol) $key and $val are the parameters of a function() or method, they are used to define what information to affect when a function() or method is executed.
If this is correct, great. But I just want to say that this can be very, very, confusing to those new to coding -- especially if the structures of functions have not yet been explained.
The basic construct of a function (to my understanding) is this:
function(x, y) { //code you want to execute// };
....and x and y are parameters.
and since list() is a function it applies to list() as well -- thus the terms $key and $val (they could be just as easily be named x or y).
So yes, if this isn't correct information, please correct me -- I actually would like the correct information after all -- but also if you could, please explain these things (functions, parameters, arguments, the structure of individual functions) in the lessons before you take us there. Thanks.
3 Answers
Amanda McNeal
9,268 PointsThis is where documentation comes in handy. :) On the PHP manual site, it explains what list() is for:
Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.
list assigns the variables for you, which is why Alena is then able to echo $key
and $val
. List looks at what you pass it, in this case, the first key value pair in $learn
(after being asorted), which is $key: 3
and $val: Arrays
.
You're correct that you could name those variables anything, for example $index
and $todo
. They're just a representation of what is in the array so that you can call those key value pairs and do something with them.
Mostafa Alhosain
7,305 PointsIn this code: while ((list($key, $val) = each($learn)) && $count++ < 2) { echo "$key => $val\n"; }
I think the only part that could cause confusion would be the {list($key, $val) = each($learn)}
So basically the each() function returns two values (the index/*key of the item and the value) so in order to display both values you used list(). [You can read the each array documentation to learn more about it.]
*The list() took two parameters (it can take more... check the documentation for more info) the first one is the $key and the second one is the $val
*These two parameters are what we want to display in the echo (the index and value ex: "1 => this is my second item!").
*These two variables $key and $val don't need to be declared outside the loop because the list() does that for us [making our life easier :) ] and each time the loop runs... the values for these variables are updated with the current array item in scope so u can use those values inside your loop to do whatever you want.
Best Of Luck, and I hope this helps.
Piotr Manczak
Front End Web Development Techdegree Graduate 29,365 PointsI share your pain. This is some sort of witchcraft. I will keep watching it till I get it. I afraid it may take a while.