1--TEST--
2Test array_shift() function : usage variations - maintaining referenced elements
3--FILE--
4<?php
5/*
6 * From a comment left by Traps on 09-Jul-2007 on the array_shift documentation page:
7 * For those that may be trying to use array_shift() with an array containing references
8 * (e.g. working with linked node trees), beware that array_shift() may not work as you expect:
9 * it will return a *copy* of the first element of the array,
10 * and not the element itself, so your reference will be lost.
11 * The solution is to reference the first element before removing it with array_shift():
12 */
13
14echo "*** Testing array_shift() : usage variations ***\n";
15
16// using only array_shift:
17echo "\n-- Reference result of array_shift: --\n";
18$a = 1;
19$array = array(&$a);
20$b =& array_shift($array);
21$b = 2;
22echo "a = $a, b = $b\n";
23
24// solution: referencing the first element first:
25echo "\n-- Reference first element before array_shift: --\n";
26$a = 1;
27$array = array(&$a);
28$b =& $array[0];
29array_shift($array);
30$b = 2;
31echo "a = $a, b = $b\n";
32
33echo "Done";
34?>
35--EXPECTF--
36*** Testing array_shift() : usage variations ***
37
38-- Reference result of array_shift: --
39
40Notice: Only variables should be assigned by reference in %s on line %d
41a = 1, b = 2
42
43-- Reference first element before array_shift: --
44a = 2, b = 2
45Done
46