json_decode_basic.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. --TEST--
  2. Test json_decode() function : basic functionality
  3. --SKIPIF--
  4. <?php if (!extension_loaded("json")) print "skip"; ?>
  5. --FILE--
  6. <?php
  7. echo "*** Testing json_decode() : basic functionality ***\n";
  8. // array with different values for $string
  9. $inputs = array (
  10. '0',
  11. '123',
  12. '-123',
  13. '2147483647',
  14. '-2147483648',
  15. '123.456',
  16. '1230',
  17. '-1230',
  18. 'true',
  19. 'false',
  20. 'null',
  21. '"abc"',
  22. '"Hello World\r\n"',
  23. '[]',
  24. '[1,2,3,4,5]',
  25. '{"myInt":99,"myFloat":123.45,"myNull":null,"myBool":true,"myString":"Hello World"}',
  26. '{"Jan":31,"Feb":29,"Mar":31,"April":30,"May":31,"June":30}',
  27. '""',
  28. '{}'
  29. );
  30. // loop through with each element of the $inputs array to test json_decode() function
  31. $count = 1;
  32. foreach($inputs as $input) {
  33. echo "-- Iteration $count --\n";
  34. var_dump(json_decode($input));
  35. var_dump(json_decode($input, true));
  36. $count++;
  37. }
  38. ?>
  39. ===Done===
  40. --EXPECTF--
  41. *** Testing json_decode() : basic functionality ***
  42. -- Iteration 1 --
  43. int(0)
  44. int(0)
  45. -- Iteration 2 --
  46. int(123)
  47. int(123)
  48. -- Iteration 3 --
  49. int(-123)
  50. int(-123)
  51. -- Iteration 4 --
  52. int(2147483647)
  53. int(2147483647)
  54. -- Iteration 5 --
  55. int(-2147483648)
  56. int(-2147483648)
  57. -- Iteration 6 --
  58. float(123.456)
  59. float(123.456)
  60. -- Iteration 7 --
  61. int(1230)
  62. int(1230)
  63. -- Iteration 8 --
  64. int(-1230)
  65. int(-1230)
  66. -- Iteration 9 --
  67. bool(true)
  68. bool(true)
  69. -- Iteration 10 --
  70. bool(false)
  71. bool(false)
  72. -- Iteration 11 --
  73. NULL
  74. NULL
  75. -- Iteration 12 --
  76. string(3) "abc"
  77. string(3) "abc"
  78. -- Iteration 13 --
  79. string(13) "Hello World
  80. "
  81. string(13) "Hello World
  82. "
  83. -- Iteration 14 --
  84. array(0) {
  85. }
  86. array(0) {
  87. }
  88. -- Iteration 15 --
  89. array(5) {
  90. [0]=>
  91. int(1)
  92. [1]=>
  93. int(2)
  94. [2]=>
  95. int(3)
  96. [3]=>
  97. int(4)
  98. [4]=>
  99. int(5)
  100. }
  101. array(5) {
  102. [0]=>
  103. int(1)
  104. [1]=>
  105. int(2)
  106. [2]=>
  107. int(3)
  108. [3]=>
  109. int(4)
  110. [4]=>
  111. int(5)
  112. }
  113. -- Iteration 16 --
  114. object(stdClass)#%d (5) {
  115. ["myInt"]=>
  116. int(99)
  117. ["myFloat"]=>
  118. float(123.45)
  119. ["myNull"]=>
  120. NULL
  121. ["myBool"]=>
  122. bool(true)
  123. ["myString"]=>
  124. string(11) "Hello World"
  125. }
  126. array(5) {
  127. ["myInt"]=>
  128. int(99)
  129. ["myFloat"]=>
  130. float(123.45)
  131. ["myNull"]=>
  132. NULL
  133. ["myBool"]=>
  134. bool(true)
  135. ["myString"]=>
  136. string(11) "Hello World"
  137. }
  138. -- Iteration 17 --
  139. object(stdClass)#%d (6) {
  140. ["Jan"]=>
  141. int(31)
  142. ["Feb"]=>
  143. int(29)
  144. ["Mar"]=>
  145. int(31)
  146. ["April"]=>
  147. int(30)
  148. ["May"]=>
  149. int(31)
  150. ["June"]=>
  151. int(30)
  152. }
  153. array(6) {
  154. ["Jan"]=>
  155. int(31)
  156. ["Feb"]=>
  157. int(29)
  158. ["Mar"]=>
  159. int(31)
  160. ["April"]=>
  161. int(30)
  162. ["May"]=>
  163. int(31)
  164. ["June"]=>
  165. int(30)
  166. }
  167. -- Iteration 18 --
  168. string(0) ""
  169. string(0) ""
  170. -- Iteration 19 --
  171. object(stdClass)#%d (0) {
  172. }
  173. array(0) {
  174. }
  175. ===Done===