array_walk_variation9.phpt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. --TEST--
  2. Test array_walk() function : usage variations - different callback functions
  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. * Passing different types of callback functions to array_walk()
  11. * without parameters
  12. * with less and more parameters
  13. */
  14. echo "*** Testing array_walk() : callback function variation ***\n";
  15. $input = array('Apple', 'Banana', 'Mango', 'Orange');
  16. echo "-- callback function with both parameters --\n";
  17. function callback_two_parameter($value, $key)
  18. {
  19. // dump the arguments to check that they are passed
  20. // with proper type
  21. var_dump($key); // key
  22. var_dump($value); // value
  23. echo "\n"; // new line to separate the output between each element
  24. }
  25. var_dump( array_walk($input, 'callback_two_parameter'));
  26. echo "-- callback function with only one parameter --\n";
  27. function callback_one_parameter($value)
  28. {
  29. // dump the arguments to check that they are passed
  30. // with proper type
  31. var_dump($value); // value
  32. echo "\n"; // new line to separate the output between each element
  33. }
  34. var_dump( array_walk($input, 'callback_one_parameter'));
  35. echo "-- callback function without parameters --\n";
  36. function callback_no_parameter()
  37. {
  38. echo "callback3() called\n";
  39. }
  40. var_dump( array_walk($input, 'callback_no_parameter'));
  41. echo "-- passing one more parameter to function with two parameters --\n";
  42. var_dump( array_walk($input, 'callback_two_parameter', 10));
  43. echo "Done"
  44. ?>
  45. --EXPECT--
  46. *** Testing array_walk() : callback function variation ***
  47. -- callback function with both parameters --
  48. int(0)
  49. string(5) "Apple"
  50. int(1)
  51. string(6) "Banana"
  52. int(2)
  53. string(5) "Mango"
  54. int(3)
  55. string(6) "Orange"
  56. bool(true)
  57. -- callback function with only one parameter --
  58. string(5) "Apple"
  59. string(6) "Banana"
  60. string(5) "Mango"
  61. string(6) "Orange"
  62. bool(true)
  63. -- callback function without parameters --
  64. callback3() called
  65. callback3() called
  66. callback3() called
  67. callback3() called
  68. bool(true)
  69. -- passing one more parameter to function with two parameters --
  70. int(0)
  71. string(5) "Apple"
  72. int(1)
  73. string(6) "Banana"
  74. int(2)
  75. string(5) "Mango"
  76. int(3)
  77. string(6) "Orange"
  78. bool(true)
  79. Done