intval.phpt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. --TEST--
  2. Test intval() function
  3. --SKIPIF--
  4. <?php
  5. if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only");
  6. ?>
  7. --FILE--
  8. <?php
  9. echo "*** Testing intval() with valid integer values ***\n";
  10. // different valid integer values
  11. $valid_ints = array(
  12. '0',
  13. '1',
  14. '-1',
  15. '-2147483648', // max negative integer value
  16. '-2147483647',
  17. 2147483647, // max positive integer value
  18. 2147483640,
  19. 0x123B, // integer as hexadecimal
  20. '0x12ab',
  21. '0Xfff',
  22. '0XFA',
  23. -0x80000000, // max negative integer as hexadecimal
  24. '0x7fffffff', // max positive integer as hexadecimal
  25. 0x7FFFFFFF, // max positive integer as hexadecimal
  26. '0123', // integer as octal
  27. 01, // should be quivalent to octal 1
  28. -020000000000, // max negative integer as octal
  29. 017777777777, // max positive integer as octal
  30. );
  31. /* loop to check that intval() recognizes different
  32. integer values, expected output:integer value in decimal notation for valid integer */
  33. echo "\n***Output with default base value ie 10 ***\n";
  34. foreach ($valid_ints as $value ) {
  35. var_dump( intval($value) );
  36. }
  37. echo "\n***Output with base value of 10( explicitly passed as argument) ***\n";
  38. foreach ($valid_ints as $value ) {
  39. var_dump( intval($value, 10) );
  40. }
  41. echo "\n***Output with base value of 16 ***\n";
  42. foreach ($valid_ints as $value ) {
  43. var_dump( intval($value, 16) );
  44. }
  45. echo "\n***Output with base value of 8 ***\n";
  46. foreach ($valid_ints as $value ) {
  47. var_dump( intval($value, 8) );
  48. }
  49. echo "\n*** Testing intval() on non integer types ***\n";
  50. // get a resource type variable
  51. $fp = fopen (__FILE__, "r");
  52. fclose($fp);
  53. $dfp = opendir ( __DIR__ );
  54. closedir($dfp);
  55. // unset variable
  56. $unset_var = 10;
  57. unset ($unset_var);
  58. // other types in a array
  59. $not_int_types = array (
  60. /* float values */
  61. '-2147483649', // float value
  62. '2147483648', // float value
  63. '-0x80000001', // float value, beyond max negative int
  64. '0x800000001', // float value, beyond max positive int
  65. '020000000001', // float value, beyond max positive int
  66. '-020000000001', // float value, beyond max negative int
  67. 0.0,
  68. -0.1,
  69. 1.0,
  70. 1e5,
  71. -1e6,
  72. 1E8,
  73. -1E9,
  74. 10.0000000000000000005,
  75. 10.5e+5,
  76. /* resources */
  77. $fp,
  78. $dfp,
  79. /* arrays */
  80. array(),
  81. array(0),
  82. array(1),
  83. array(NULL),
  84. array(null),
  85. array("string"),
  86. array(true),
  87. array(TRUE),
  88. array(false),
  89. array(FALSE),
  90. array(1,2,3,4),
  91. array(1 => "One", "two" => 2),
  92. /* strings */
  93. "",
  94. '',
  95. "0",
  96. '0',
  97. "1",
  98. '1',
  99. "\x01",
  100. '\x01',
  101. "\01",
  102. '\01',
  103. 'string',
  104. "string",
  105. "true",
  106. "FALSE",
  107. 'false',
  108. 'TRUE',
  109. "NULL",
  110. 'null',
  111. /* booleans */
  112. true,
  113. false,
  114. TRUE,
  115. FALSE,
  116. /* undefined and unset vars */
  117. @$unset_var,
  118. @$undefined_var
  119. );
  120. /* loop through the $not_int_types to see working of
  121. intval() on non integer types, expected output: integer value in decimal notation for valid integers */
  122. foreach ($not_int_types as $type ) {
  123. var_dump( intval($type) );
  124. }
  125. echo "\n--- Done ---\n";
  126. ?>
  127. --EXPECTF--
  128. *** Testing intval() with valid integer values ***
  129. ***Output with default base value ie 10 ***
  130. int(0)
  131. int(1)
  132. int(-1)
  133. int(-2147483648)
  134. int(-2147483647)
  135. int(2147483647)
  136. int(2147483640)
  137. int(4667)
  138. int(0)
  139. int(0)
  140. int(0)
  141. int(-2147483648)
  142. int(0)
  143. int(2147483647)
  144. int(123)
  145. int(1)
  146. int(-2147483648)
  147. int(2147483647)
  148. ***Output with base value of 10( explicitly passed as argument) ***
  149. int(0)
  150. int(1)
  151. int(-1)
  152. int(-2147483648)
  153. int(-2147483647)
  154. int(2147483647)
  155. int(2147483640)
  156. int(4667)
  157. int(0)
  158. int(0)
  159. int(0)
  160. int(-2147483648)
  161. int(0)
  162. int(2147483647)
  163. int(123)
  164. int(1)
  165. int(-2147483648)
  166. int(2147483647)
  167. ***Output with base value of 16 ***
  168. int(0)
  169. int(1)
  170. int(-1)
  171. int(-2147483648)
  172. int(-2147483648)
  173. int(2147483647)
  174. int(2147483640)
  175. int(4667)
  176. int(4779)
  177. int(4095)
  178. int(250)
  179. int(-2147483648)
  180. int(2147483647)
  181. int(2147483647)
  182. int(291)
  183. int(1)
  184. int(-2147483648)
  185. int(2147483647)
  186. ***Output with base value of 8 ***
  187. int(0)
  188. int(1)
  189. int(-1)
  190. int(-9020)
  191. int(-9020)
  192. int(2147483647)
  193. int(2147483640)
  194. int(4667)
  195. int(0)
  196. int(0)
  197. int(0)
  198. int(-2147483648)
  199. int(0)
  200. int(2147483647)
  201. int(83)
  202. int(1)
  203. int(-2147483648)
  204. int(2147483647)
  205. *** Testing intval() on non integer types ***
  206. int(-2147483648)
  207. int(2147483647)
  208. int(0)
  209. int(0)
  210. int(2147483647)
  211. int(-2147483648)
  212. int(0)
  213. int(0)
  214. int(1)
  215. int(100000)
  216. int(-1000000)
  217. int(100000000)
  218. int(-1000000000)
  219. int(10)
  220. int(1050000)
  221. int(%d)
  222. int(%d)
  223. int(0)
  224. int(1)
  225. int(1)
  226. int(1)
  227. int(1)
  228. int(1)
  229. int(1)
  230. int(1)
  231. int(1)
  232. int(1)
  233. int(1)
  234. int(1)
  235. int(0)
  236. int(0)
  237. int(0)
  238. int(0)
  239. int(1)
  240. int(1)
  241. int(0)
  242. int(0)
  243. int(0)
  244. int(0)
  245. int(0)
  246. int(0)
  247. int(0)
  248. int(0)
  249. int(0)
  250. int(0)
  251. int(0)
  252. int(0)
  253. int(1)
  254. int(0)
  255. int(1)
  256. int(0)
  257. int(0)
  258. int(0)
  259. --- Done ---