PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/slow/php7_backported/is_numeric.php

http://github.com/facebook/hiphop-php
PHP | 155 lines | 132 code | 8 blank | 15 comment | 0 complexity | 8a386a5674fd5bfccd99c37089e4386a 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. 123.56e30,
  33. 123.56E30,
  34. 426.45e-30,
  35. 5657.3E-40,
  36. 3486.36e+40,
  37. 3486.36E+90,
  38. -3486.36E+10,
  39. -3486.36e+80,
  40. -426.45e-50,
  41. -426.45E-99,
  42. 1e2,
  43. -1e2,
  44. -1e-2,
  45. +1e2,
  46. +1e+2,
  47. +1e-2,
  48. +1e+2,
  49. 2245555555555555.444,
  50. 1.444444444444444444,
  51. 0xff, // hexa decimal numbers
  52. 0xFF,
  53. //0x1111111111111111111111,
  54. -0x1111111,
  55. +0x6698319,
  56. 01000000000000000000000,
  57. 0123,
  58. 0345,
  59. -0200001,
  60. -0200001.7,
  61. 0200001.7,
  62. +0200001,
  63. +0200001.7,
  64. +0200001.7,
  65. 2.00000000000000000000001, // a float value with more precision points
  66. "1", // numeric in the form of string
  67. "-1",
  68. "1e2",
  69. " 1",
  70. "2974394749328742328432",
  71. "-1e-2",
  72. '1',
  73. '-1',
  74. '1e2',
  75. ' 1',
  76. '2974394749328742328432',
  77. '-1e-2',
  78. "0123",
  79. '0123',
  80. "-0123",
  81. "+0123",
  82. '-0123',
  83. '+0123'
  84. ];
  85. /* loop to check that is_numeric() recognizes different
  86. numeric values, expected output: bool(true) */
  87. $loop_counter = 1;
  88. foreach ($numerics as $num ) {
  89. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  90. var_dump( is_numeric($num) );
  91. }
  92. echo "\n*** Testing is_numeric() on non numeric types ***\n";
  93. // get a resource type variable
  94. $fp = fopen (__FILE__, "r");
  95. $dfp = opendir ( dirname(__FILE__) );
  96. // unset variable
  97. $unset_var = 10.5;
  98. unset ($unset_var);
  99. // other types in a array
  100. $not_numerics = varray[
  101. "0x80001",
  102. "-0x80001",
  103. "+0x80001",
  104. "-0x80001.5",
  105. "0x80001.5",
  106. new stdclass, // object
  107. $fp, // resource
  108. $dfp,
  109. varray[],
  110. varray["string"],
  111. "",
  112. "1 ",
  113. "- 1",
  114. "1.2.4",
  115. "1e7.6",
  116. "3FF",
  117. "20 test",
  118. "3.6test",
  119. "1,000",
  120. "NULL",
  121. "true",
  122. true,
  123. NULL,
  124. null,
  125. TRUE,
  126. FALSE,
  127. false,
  128. @$unset_var, // unset variable
  129. @$undefined_var
  130. ];
  131. /* loop through the $not_numerics to see working of
  132. is_numeric() on non numeric values, expected output: bool(false) */
  133. $loop_counter = 1;
  134. foreach ($not_numerics as $type ) {
  135. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  136. var_dump( is_numeric($type) );
  137. }
  138. echo "\n*** Testing error conditions ***\n";
  139. //Zero argument
  140. try { var_dump( is_numeric() ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  141. //arguments more than expected
  142. try { var_dump( is_numeric("10", "20") ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  143. echo "Done\n";
  144. // close the resources used
  145. fclose($fp);
  146. closedir($dfp);
  147. }