list() is behaving logical and weird at the same time

<?php
list($a, $b) = array(1, 2, 3, 4);
?>

What direction will list start processing the array? You'd expect that it'd be left to right and that's right: $a = 1, $b = 2. But where it gets confusing:

<?php
list($a, $a) = array(1, 2, 3, 4);
?>

Well, we already know list will only consider 1 and 2. So first 1 gets assigned to $a and then 2 gets assigned and that's where our story ends, right? After all, the second $a is where $b used to be, right? Wrong. The manual warns you

list() assigns the values starting with the right-most parameter.

So first $a becomes 2 and then 1. And so $a becomes 1.

This is where writing a manual becomes hard -- are you going to add a counter-warning that the slicing does behave normally? What kind of warning is that, anyways: "there is nothing to see here, move on". Do you just let this slip and hope people get that the abscence of the warning means there's nothing to be warned about? Or in the light of a strange behavior normal behavior merits a warning?

Actually, if you want to confuse someone at a tech interview, just throw list($a, $a) = array(1, 2, 3, 4); and point out how 1, 2, 3, 4 are all possible depending on where list starts processing the left and the right side -- if the left side is processed from right-to-left why not the right side too? (Because it makes no sense to do that!).

Comments

At all I do not know, that here and to tell that it is possible

I am sorry, that has interfered... At me a similar situation. Let's discuss. Write here or in PM.

At all I do not know, that here and to tell that it is possible

I am sorry, that has interfered... At me a similar situation. Let's discuss. Write here or in PM.

wow

wowo thanks

Making Sense ...

Thanks for exposing this subject. And now to add some clarity:

Only two of the values will have opcodes created for this multiple assignment operation and relate to elements 0 and 1 of the array. list construct expects array to have sequential numeric keys starting with zero, even if the keys become disordered from sorting. So, values 3 and 4 get sidelined. Because list is right associative it takes element 1 and assigns it to the right most variable $a. Next, it does the same with element 0 assigning it to the remaining $a. The leftmost $a overwrites the rightmost $a b/c the "lefty" was assigned last! The list operator is right associative since simple assignment is right associative, therefore for a multiple assignment the same holds true and keeps assignment associativity consistent.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options