PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/good/ext/standard/tests/general_functions/is_numeric.php

http://github.com/facebook/hiphop-php
PHP | 158 lines | 135 code | 8 blank | 15 comment | 0 complexity | d77f6a2fef3e29e554435723e82f85ef MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. /* Prototype: bool is_numeric ( mixed $var );
  3. * Description: Finds whether a variable is a number or a numeric string
  4. */
  5. <<__EntryPoint>> function main(): void {
  6. echo "*** Testing is_numeric() with valid numeric values ***\n";
  7. // different valid numeric vlaues
  8. $numerics = varray[
  9. 0,
  10. 1,
  11. -1,
  12. -0,
  13. +0,
  14. 0.0,
  15. -0.0,
  16. +0.0,
  17. 1.0,
  18. -1.0,
  19. +1.0,
  20. .5,
  21. -.5,
  22. +.5,
  23. -.5e-2,
  24. .5e-2,
  25. +.5e-2,
  26. +.5E+2,
  27. 0.70000000,
  28. +0.70000000,
  29. -0.70000000,
  30. 1234567890123456,
  31. -1234567890123456,
  32. PHP_INT_MAX,
  33. PHP_INT_MIN,
  34. 123.56e30,
  35. 123.56E30,
  36. 426.45e-30,
  37. 5657.3E-40,
  38. 3486.36e+40,
  39. 3486.36E+90,
  40. -3486.36E+10,
  41. -3486.36e+80,
  42. -426.45e-50,
  43. -426.45E-99,
  44. 1e2,
  45. -1e2,
  46. -1e-2,
  47. +1e2,
  48. +1e+2,
  49. +1e-2,
  50. +1e+2,
  51. 2245555555555555.444,
  52. 1.444444444444444444,
  53. 0xff, // hexa decimal numbers
  54. 0xFF,
  55. //0x1111111111111111111111,
  56. -0x1111111,
  57. +0x6698319,
  58. 01000000000000000000000,
  59. 0123,
  60. 0345900,
  61. -0200001,
  62. -0200001.7,
  63. 0200001.7,
  64. +0200001,
  65. +0200001.7,
  66. +0200001.7,
  67. 2.00000000000000000000001, // a float value with more precision points
  68. "1", // numeric in the form of string
  69. "-1",
  70. "1e2",
  71. " 1",
  72. "2974394749328742328432",
  73. "-1e-2",
  74. '1',
  75. '-1',
  76. '1e2',
  77. ' 1',
  78. '2974394749328742328432',
  79. '-1e-2',
  80. "0xff",
  81. '0xff',
  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 = varray[
  105. "-0x80001",
  106. "+0x80001",
  107. "-0x80001.5",
  108. "0x80001.5",
  109. new stdclass, // object
  110. $fp, // resource
  111. $dfp,
  112. varray[],
  113. varray["string"],
  114. "",
  115. "1 ",
  116. "- 1",
  117. "1.2.4",
  118. "1e7.6",
  119. "3FF",
  120. "20 test",
  121. "3.6test",
  122. "1,000",
  123. "NULL",
  124. "true",
  125. true,
  126. NULL,
  127. null,
  128. TRUE,
  129. FALSE,
  130. false,
  131. @$unset_var, // unset variable
  132. @$undefined_var
  133. ];
  134. /* loop through the $not_numerics to see working of
  135. is_numeric() on non numeric values, expected output: bool(false) */
  136. $loop_counter = 1;
  137. foreach ($not_numerics as $type ) {
  138. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  139. var_dump( is_numeric($type) );
  140. }
  141. echo "\n*** Testing error conditions ***\n";
  142. //Zero argument
  143. try { var_dump( is_numeric() ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  144. //arguments more than expected
  145. try { var_dump( is_numeric("10", "20") ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  146. echo "Done\n";
  147. // close the resources used
  148. fclose($fp);
  149. closedir($dfp);
  150. }