array_slice_variation9.phpt 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. --TEST--
  2. Test array_slice() function : usage variations - referenced variables
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_slice(array $input, int $offset [, int $length [, bool $preserve_keys]])
  6. * Description: Returns elements specified by offset and length
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Test array_slice() when:
  11. * 1. Passed an array of referenced variables
  12. * 2. $input argument is passed by reference
  13. */
  14. echo "*** Testing array_slice() : usage variations ***\n";
  15. $val1 = 'one';
  16. $val2 = 'two';
  17. $val3 = 'three';
  18. echo "\n-- Array of referenced variables (\$preserve_keys = default) --\n";
  19. $input = array(3 => &$val1, 2 => &$val2, 1 => &$val3);
  20. var_dump(array_slice($input, 1, 2));
  21. echo "-- Change \$val2 (\$preserve_keys = TRUE) --\n";
  22. $val2 = 'hello, world';
  23. var_dump(array_slice($input, 1, 2, true));
  24. echo "Done";
  25. ?>
  26. --EXPECTF--
  27. *** Testing array_slice() : usage variations ***
  28. -- Array of referenced variables ($preserve_keys = default) --
  29. array(2) {
  30. [0]=>
  31. &string(3) "two"
  32. [1]=>
  33. &string(5) "three"
  34. }
  35. -- Change $val2 ($preserve_keys = TRUE) --
  36. array(2) {
  37. [2]=>
  38. &string(12) "hello, world"
  39. [1]=>
  40. &string(5) "three"
  41. }
  42. Done