array_walk_variation5.phpt 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --TEST--
  2. Test array_walk() function : usage variations - 'input' argument containing reference variables
  3. --FILE--
  4. <?php
  5. /* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata])
  6. * Description: Apply a user function to every member of an array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Testing array_walk() with an array having reference variables
  11. */
  12. echo "*** Testing array_walk() : array with references ***\n";
  13. $value1 = 10;
  14. $value2 = -20;
  15. $value3 = &$value1;
  16. $value4 = 50;
  17. // 'input' array containing references to above variables
  18. $input = array(&$value1, &$value2, -35, &$value3, 0, &$value4);
  19. // callback function
  20. /* Prototype : callback(int $value, mixed $key)
  21. * Parameters : $value - values in given input array
  22. * $key - keys in given input array
  23. * Description : function checks for the value whether positive or negative and displays according to that
  24. */
  25. function callback($value, $key)
  26. {
  27. // dump the arguments to check that they are passed
  28. // with proper type
  29. var_dump($key); // key
  30. var_dump($value); // value
  31. echo "\n"; // new line to separate the output between each element
  32. }
  33. var_dump( array_walk($input, "callback"));
  34. echo "Done"
  35. ?>
  36. --EXPECTF--
  37. *** Testing array_walk() : array with references ***
  38. int(0)
  39. int(10)
  40. int(1)
  41. int(-20)
  42. int(2)
  43. int(-35)
  44. int(3)
  45. int(10)
  46. int(4)
  47. int(0)
  48. int(5)
  49. int(50)
  50. bool(true)
  51. Done