PHP PHP Arrays and Control Structures PHP Loops While Listing Array Values

Where 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
Amanda McNeal
9,256 Points

This 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.

In 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.

I 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.