array_map_variation10.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. --TEST--
  2. Test array_map() function : usage variations - anonymous callback function
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_map ( callback $callback , array $arr1 [, array $... ] )
  6. * Description: Applies the callback to the elements of the given arrays
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Test array_map() by passing anoymous callback function with following variations
  11. */
  12. echo "*** Testing array_map() : anonymous callback function ***\n";
  13. $array1 = array(1, 2, 3);
  14. $array2 = array(3, 4, 5);
  15. echo "-- anonymous function with all parameters and body --\n";
  16. var_dump( array_map( create_function('$a, $b', 'return array($a, $b);'), $array1, $array2));
  17. echo "-- anonymous function with two parameters and passing one array --\n";
  18. var_dump( array_map( create_function('$a, $b', 'return array($a, $b);'), $array1));
  19. echo "-- anonymous function with NULL parameter --\n";
  20. var_dump( array_map( create_function(NULL, 'return NULL;'), $array1));
  21. echo "-- anonymous function with NULL body --\n";
  22. var_dump( array_map( create_function('$a', NULL), $array1));
  23. echo "-- passing NULL as 'arr1' --\n";
  24. var_dump( array_map( create_function('$a', 'return array($a);'), NULL));
  25. echo "Done";
  26. ?>
  27. --EXPECTF--
  28. *** Testing array_map() : anonymous callback function ***
  29. -- anonymous function with all parameters and body --
  30. array(3) {
  31. [0]=>
  32. array(2) {
  33. [0]=>
  34. int(1)
  35. [1]=>
  36. int(3)
  37. }
  38. [1]=>
  39. array(2) {
  40. [0]=>
  41. int(2)
  42. [1]=>
  43. int(4)
  44. }
  45. [2]=>
  46. array(2) {
  47. [0]=>
  48. int(3)
  49. [1]=>
  50. int(5)
  51. }
  52. }
  53. -- anonymous function with two parameters and passing one array --
  54. Warning: Missing argument 2 for __lambda_func() in %s(20) : runtime-created function on line %d
  55. Notice: Undefined variable: b in %s(20) : runtime-created function on line %d
  56. Warning: Missing argument 2 for __lambda_func() in %s(20) : runtime-created function on line %d
  57. Notice: Undefined variable: b in %s(20) : runtime-created function on line %d
  58. Warning: Missing argument 2 for __lambda_func() in %s(20) : runtime-created function on line %d
  59. Notice: Undefined variable: b in %s(20) : runtime-created function on line %d
  60. array(3) {
  61. [0]=>
  62. array(2) {
  63. [0]=>
  64. int(1)
  65. [1]=>
  66. NULL
  67. }
  68. [1]=>
  69. array(2) {
  70. [0]=>
  71. int(2)
  72. [1]=>
  73. NULL
  74. }
  75. [2]=>
  76. array(2) {
  77. [0]=>
  78. int(3)
  79. [1]=>
  80. NULL
  81. }
  82. }
  83. -- anonymous function with NULL parameter --
  84. array(3) {
  85. [0]=>
  86. NULL
  87. [1]=>
  88. NULL
  89. [2]=>
  90. NULL
  91. }
  92. -- anonymous function with NULL body --
  93. array(3) {
  94. [0]=>
  95. NULL
  96. [1]=>
  97. NULL
  98. [2]=>
  99. NULL
  100. }
  101. -- passing NULL as 'arr1' --
  102. Warning: array_map(): Argument #2 should be an array in %s on line %d
  103. NULL
  104. Done