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