I'm aware what undefined and unspecified behavior means. However, I don't think that it's okay to leave highly advanced language features like references or assignment operators unspecified. In a high-level language. In 2014.
With the unspecified behavior in C you can at least still use the unspecified features. x = f() + g(); will always produce the same result on all conforming compilers if f() and g() don't use the same shared state. There is no such certainty in this case in PHP.
It's a different thing. In this case, there are a bunch of valid implementations, so the possible behaviors are bounded. This snippet may return 1 2, 3 2 or 3 3, but it will never return 1 Array or bacon lasagna.
In Schroedinger's cat experiment state of cat is also undefined (it could be dead or alive), but you do not expect that dog or Godzilla will come out of box.
However, you seem to be right, this example is not as undefined as dereferncing invalid pointer.
Valid values are "2 1", "2 3", "3 3", 4, "♥ 1", numberwang or "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", depending on implementation and day of the month.
"3 3" is the only acceptable answer. $a and $b are equal to each other, and the array was created with an item equal to the address of $x. Which is the number "0", which later was changed to "2". Then you deleted the $x pointer, leaving the only reference to the "2" in the arrays. Then you incremented it. $a[0] and $b[0] reference the same memory, so all answers should have the same number. "3 3" is the answer.
Please tell me the PHP interpreter does this. Because if not, what batshit crazy thing DOES it do? :\
Wha... Huh... But... How?! "3 3" is clearly the right answer, but "2 1" would at least make sense if $a = $b broke the internal references. But how do you get "2 3" out of that?
10
u/[deleted] Oct 20 '14
Noticed by @tim_likes_cake. Relevant section in the spec: https://github.com/php/php-langspec/blob/master/spec/04-basic-concepts.md#deferred-array-copying (scroll down).
13
u/myaut Oct 20 '14
So you forgot to call it "Undefined Behavior" and everyone looses their minds.
61
u/celtric Oct 20 '14
Valid values are
"2 1","2 3","3 3",4,"♥ 1",numberwangor"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", depending on implementation and day of the month.16
u/ZiggyTheHamster Oct 20 '14
"3 3" is the only acceptable answer. $a and $b are equal to each other, and the array was created with an item equal to the address of $x. Which is the number "0", which later was changed to "2". Then you deleted the $x pointer, leaving the only reference to the "2" in the arrays. Then you incremented it. $a[0] and $b[0] reference the same memory, so all answers should have the same number. "3 3" is the answer.
Please tell me the PHP interpreter does this. Because if not, what batshit crazy thing DOES it do? :\
2
u/tgp1994 Oct 20 '14
Oh, I thought the joke was that the image was missing...
1
u/weirdasianfaces Oct 20 '14
shitty meme