Annotation of embedaddon/php/ext/standard/tests/array/array_shift_variation8.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test 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:
! 19: echo "*** Testing array_shift() : usage variations ***\n";
! 20:
! 21: // using only array_shift:
! 22: echo "\n-- Reference result of array_shift: --\n";
! 23: $a = 1;
! 24: $array = array(&$a);
! 25: $b =& array_shift($array);
! 26: $b = 2;
! 27: echo "a = $a, b = $b\n";
! 28:
! 29: // solution: referencing the first element first:
! 30: echo "\n-- Reference first element before array_shift: --\n";
! 31: $a = 1;
! 32: $array = array(&$a);
! 33: $b =& $array[0];
! 34: array_shift($array);
! 35: $b = 2;
! 36: echo "a = $a, b = $b\n";
! 37:
! 38: echo "Done";
! 39: ?>
! 40: --EXPECTF--
! 41: *** Testing array_shift() : usage variations ***
! 42:
! 43: -- Reference result of array_shift: --
! 44:
! 45: Strict Standards: Only variables should be assigned by reference in %s on line %d
! 46: a = 1, b = 2
! 47:
! 48: -- Reference first element before array_shift: --
! 49: a = 2, b = 2
! 50: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>