is_numeric.phpt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. --TEST--
  2. Test is_numeric() function
  3. --FILE--
  4. <?php
  5. /* Prototype: bool is_numeric ( mixed $var );
  6. * Description: Finds whether a variable is a number or a numeric string
  7. */
  8. echo "*** Testing is_numeric() with valid numeric values ***\n";
  9. // different valid numeric values
  10. $numerics = array(
  11. 0,
  12. 1,
  13. -1,
  14. -0,
  15. +0,
  16. 0.0,
  17. -0.0,
  18. +0.0,
  19. 1.0,
  20. -1.0,
  21. +1.0,
  22. .5,
  23. -.5,
  24. +.5,
  25. -.5e-2,
  26. .5e-2,
  27. +.5e-2,
  28. +.5E+2,
  29. 0.70000000,
  30. +0.70000000,
  31. -0.70000000,
  32. 1234567890123456,
  33. -1234567890123456,
  34. 984847472827282718178,
  35. -984847472827282718178,
  36. 123.56e30,
  37. 123.56E30,
  38. 426.45e-30,
  39. 5657.3E-40,
  40. 3486.36e+40,
  41. 3486.36E+90,
  42. -3486.36E+10,
  43. -3486.36e+80,
  44. -426.45e-50,
  45. -426.45E-99,
  46. 1e2,
  47. -1e2,
  48. -1e-2,
  49. +1e2,
  50. +1e+2,
  51. +1e-2,
  52. +1e+2,
  53. 2245555555555555.444,
  54. 1.444444444444444444,
  55. 0xff, // hexa decimal numbers
  56. 0xFF,
  57. //0x1111111111111111111111,
  58. -0x1111111,
  59. +0x6698319,
  60. 01000000000000000000000,
  61. 0123,
  62. 0345,
  63. -0200001,
  64. -0200001.7,
  65. 0200001.7,
  66. +0200001,
  67. +0200001.7,
  68. +0200001.7,
  69. 2.00000000000000000000001, // a float value with more precision points
  70. "1", // numeric in the form of string
  71. "-1",
  72. "1e2",
  73. " 1",
  74. "2974394749328742328432",
  75. "-1e-2",
  76. '1',
  77. '-1',
  78. '1e2',
  79. ' 1',
  80. '2974394749328742328432',
  81. '-1e-2',
  82. "0123",
  83. '0123',
  84. "-0123",
  85. "+0123",
  86. '-0123',
  87. '+0123'
  88. );
  89. /* loop to check that is_numeric() recognizes different
  90. numeric values, expected output: bool(true) */
  91. $loop_counter = 1;
  92. foreach ($numerics as $num ) {
  93. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  94. var_dump( is_numeric($num) );
  95. }
  96. echo "\n*** Testing is_numeric() on non numeric types ***\n";
  97. // get a resource type variable
  98. $fp = fopen (__FILE__, "r");
  99. $dfp = opendir ( dirname(__FILE__) );
  100. // unset variable
  101. $unset_var = 10.5;
  102. unset ($unset_var);
  103. // other types in a array
  104. $not_numerics = array(
  105. "0x80001",
  106. "-0x80001",
  107. "+0x80001",
  108. "-0x80001.5",
  109. "0x80001.5",
  110. new stdclass, // object
  111. $fp, // resource
  112. $dfp,
  113. array(),
  114. array("string"),
  115. "",
  116. "1 ",
  117. "- 1",
  118. "1.2.4",
  119. "1e7.6",
  120. "3FF",
  121. "20 test",
  122. "3.6test",
  123. "1,000",
  124. "NULL",
  125. "true",
  126. true,
  127. NULL,
  128. null,
  129. TRUE,
  130. FALSE,
  131. false,
  132. @$unset_var, // unset variable
  133. @$undefined_var
  134. );
  135. /* loop through the $not_numerics to see working of
  136. is_numeric() on non numeric values, expected output: bool(false) */
  137. $loop_counter = 1;
  138. foreach ($not_numerics as $type ) {
  139. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  140. var_dump( is_numeric($type) );
  141. }
  142. echo "\n*** Testing error conditions ***\n";
  143. //Zero argument
  144. var_dump( is_numeric() );
  145. //arguments more than expected
  146. var_dump( is_numeric("10", "20") );
  147. echo "Done\n";
  148. // close the resources used
  149. fclose($fp);
  150. closedir($dfp);
  151. ?>
  152. --EXPECTF--
  153. *** Testing is_numeric() with valid numeric values ***
  154. -- Iteration 1 --
  155. bool(true)
  156. -- Iteration 2 --
  157. bool(true)
  158. -- Iteration 3 --
  159. bool(true)
  160. -- Iteration 4 --
  161. bool(true)
  162. -- Iteration 5 --
  163. bool(true)
  164. -- Iteration 6 --
  165. bool(true)
  166. -- Iteration 7 --
  167. bool(true)
  168. -- Iteration 8 --
  169. bool(true)
  170. -- Iteration 9 --
  171. bool(true)
  172. -- Iteration 10 --
  173. bool(true)
  174. -- Iteration 11 --
  175. bool(true)
  176. -- Iteration 12 --
  177. bool(true)
  178. -- Iteration 13 --
  179. bool(true)
  180. -- Iteration 14 --
  181. bool(true)
  182. -- Iteration 15 --
  183. bool(true)
  184. -- Iteration 16 --
  185. bool(true)
  186. -- Iteration 17 --
  187. bool(true)
  188. -- Iteration 18 --
  189. bool(true)
  190. -- Iteration 19 --
  191. bool(true)
  192. -- Iteration 20 --
  193. bool(true)
  194. -- Iteration 21 --
  195. bool(true)
  196. -- Iteration 22 --
  197. bool(true)
  198. -- Iteration 23 --
  199. bool(true)
  200. -- Iteration 24 --
  201. bool(true)
  202. -- Iteration 25 --
  203. bool(true)
  204. -- Iteration 26 --
  205. bool(true)
  206. -- Iteration 27 --
  207. bool(true)
  208. -- Iteration 28 --
  209. bool(true)
  210. -- Iteration 29 --
  211. bool(true)
  212. -- Iteration 30 --
  213. bool(true)
  214. -- Iteration 31 --
  215. bool(true)
  216. -- Iteration 32 --
  217. bool(true)
  218. -- Iteration 33 --
  219. bool(true)
  220. -- Iteration 34 --
  221. bool(true)
  222. -- Iteration 35 --
  223. bool(true)
  224. -- Iteration 36 --
  225. bool(true)
  226. -- Iteration 37 --
  227. bool(true)
  228. -- Iteration 38 --
  229. bool(true)
  230. -- Iteration 39 --
  231. bool(true)
  232. -- Iteration 40 --
  233. bool(true)
  234. -- Iteration 41 --
  235. bool(true)
  236. -- Iteration 42 --
  237. bool(true)
  238. -- Iteration 43 --
  239. bool(true)
  240. -- Iteration 44 --
  241. bool(true)
  242. -- Iteration 45 --
  243. bool(true)
  244. -- Iteration 46 --
  245. bool(true)
  246. -- Iteration 47 --
  247. bool(true)
  248. -- Iteration 48 --
  249. bool(true)
  250. -- Iteration 49 --
  251. bool(true)
  252. -- Iteration 50 --
  253. bool(true)
  254. -- Iteration 51 --
  255. bool(true)
  256. -- Iteration 52 --
  257. bool(true)
  258. -- Iteration 53 --
  259. bool(true)
  260. -- Iteration 54 --
  261. bool(true)
  262. -- Iteration 55 --
  263. bool(true)
  264. -- Iteration 56 --
  265. bool(true)
  266. -- Iteration 57 --
  267. bool(true)
  268. -- Iteration 58 --
  269. bool(true)
  270. -- Iteration 59 --
  271. bool(true)
  272. -- Iteration 60 --
  273. bool(true)
  274. -- Iteration 61 --
  275. bool(true)
  276. -- Iteration 62 --
  277. bool(true)
  278. -- Iteration 63 --
  279. bool(true)
  280. -- Iteration 64 --
  281. bool(true)
  282. -- Iteration 65 --
  283. bool(true)
  284. -- Iteration 66 --
  285. bool(true)
  286. -- Iteration 67 --
  287. bool(true)
  288. -- Iteration 68 --
  289. bool(true)
  290. -- Iteration 69 --
  291. bool(true)
  292. -- Iteration 70 --
  293. bool(true)
  294. -- Iteration 71 --
  295. bool(true)
  296. -- Iteration 72 --
  297. bool(true)
  298. -- Iteration 73 --
  299. bool(true)
  300. -- Iteration 74 --
  301. bool(true)
  302. -- Iteration 75 --
  303. bool(true)
  304. -- Iteration 76 --
  305. bool(true)
  306. *** Testing is_numeric() on non numeric types ***
  307. -- Iteration 1 --
  308. bool(false)
  309. -- Iteration 2 --
  310. bool(false)
  311. -- Iteration 3 --
  312. bool(false)
  313. -- Iteration 4 --
  314. bool(false)
  315. -- Iteration 5 --
  316. bool(false)
  317. -- Iteration 6 --
  318. bool(false)
  319. -- Iteration 7 --
  320. bool(false)
  321. -- Iteration 8 --
  322. bool(false)
  323. -- Iteration 9 --
  324. bool(false)
  325. -- Iteration 10 --
  326. bool(false)
  327. -- Iteration 11 --
  328. bool(false)
  329. -- Iteration 12 --
  330. bool(false)
  331. -- Iteration 13 --
  332. bool(false)
  333. -- Iteration 14 --
  334. bool(false)
  335. -- Iteration 15 --
  336. bool(false)
  337. -- Iteration 16 --
  338. bool(false)
  339. -- Iteration 17 --
  340. bool(false)
  341. -- Iteration 18 --
  342. bool(false)
  343. -- Iteration 19 --
  344. bool(false)
  345. -- Iteration 20 --
  346. bool(false)
  347. -- Iteration 21 --
  348. bool(false)
  349. -- Iteration 22 --
  350. bool(false)
  351. -- Iteration 23 --
  352. bool(false)
  353. -- Iteration 24 --
  354. bool(false)
  355. -- Iteration 25 --
  356. bool(false)
  357. -- Iteration 26 --
  358. bool(false)
  359. -- Iteration 27 --
  360. bool(false)
  361. -- Iteration 28 --
  362. bool(false)
  363. -- Iteration 29 --
  364. bool(false)
  365. *** Testing error conditions ***
  366. Warning: is_numeric() expects exactly 1 parameter, 0 given in %s on line %d
  367. NULL
  368. Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line %d
  369. NULL
  370. Done