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.

You're just doing it wrong.

You're just doing it wrong. You're overwriting he first $a with the last $a.

Losing weight can be a very

I am really sorry i did not

I am really sorry i did not understand anything form this code .What about this Code please let me know anyone ?
My website: http://www.innovativehs.com

Its like you read my mind!

Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is great blog. A great readFootball Online Games

I am very happy to read this.

I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation t hat's at the other blogs. Appreciate your sharing this best posting. Berita Liga Spanyol

This is a fantastic website

This is a fantastic website and I can not recommend you guys enough. Full of useful resource and great layout very easy on the eyes. Please do keep up this great work... Midsummer Day

Great post you should also

Great post you should also posts about valentines day 2015 and advance
happy valentines day

Happy Valentines Day 2015

Valentine's Day.

Many thanks a lot. You really a wonderful write-up that you posted. Content that you’re undertaking like wonderful share for people like us, visitors, made the day.

Latest Love Calculator for Valentine Day to True Love Meter

thanks web web web web web

thanks
web
web
web
web
web

Valentines day status for Her wechat

good for interview

boşanma avukatı

Boşanma avukatı, İstanbul Boşanma avukatı, boşanma avukatı istanbul, boşanma, avukat arama En İyi Boşanma Avukatı

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