strval.phpt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. --TEST--
  2. Test strval() function
  3. --INI--
  4. precision=14
  5. --FILE--
  6. <?php
  7. echo "*** Testing str_val() with scalar values***\n";
  8. $heredoc_string = <<<EOD
  9. This is a multiline heredoc
  10. string. Numeric = 1232455.
  11. EOD;
  12. /* heredoc string with only numeric values */
  13. $heredoc_numeric_string = <<<EOD
  14. 12345
  15. 2345
  16. EOD;
  17. /* null heredoc string */
  18. $heredoc_empty_string = <<<EOD
  19. EOD;
  20. /* heredoc string with NULL */
  21. $heredoc_NULL_string = <<<EOD
  22. NULL
  23. EOD;
  24. // different valid scalar values
  25. $scalars = array(
  26. /* integers */
  27. 0,
  28. 1,
  29. -1,
  30. -2147483648, // max negative integer value
  31. -2147483647,
  32. 2147483647, // max positive integer value
  33. 2147483640,
  34. 0x123B, // integer as hexadecimal
  35. 0x12ab,
  36. 0Xfff,
  37. 0XFA,
  38. /* floats */
  39. -0x80000000, // max negative integer as hexadecimal
  40. 0x7fffffff, // max positive integer as hexadecimal
  41. 0x7FFFFFFF, // max positive integer as hexadecimal
  42. 0123, // integer as octal
  43. 01, // should be quivalent to octal 1
  44. -020000000000, // max negative integer as octal
  45. 017777777777, // max positive integer as octal
  46. -2147483649, // float value
  47. 2147483648, // float value
  48. -0x80000001, // float value, beyond max negative int
  49. 0x800000001, // float value, beyond max positive int
  50. 020000000001, // float value, beyond max positive int
  51. -020000000001, // float value, beyond max negative int
  52. 0.0,
  53. -0.1,
  54. 10.0000000000000000005,
  55. 10.5e+5,
  56. 1e-5,
  57. .5e+7,
  58. .6e-19,
  59. .05E+44,
  60. .0034E-30,
  61. /* booleans */
  62. true,
  63. TRUE,
  64. FALSE,
  65. false,
  66. /* strings */
  67. "",
  68. '',
  69. " ",
  70. ' ',
  71. '0',
  72. "0",
  73. "testing",
  74. "0x564",
  75. "0123",
  76. "new\n",
  77. 'new\n',
  78. "@#$$%^&&*()",
  79. " ",
  80. "null",
  81. 'null',
  82. 'true',
  83. "true",
  84. /*"\0123",
  85. "\0x12FF",*/
  86. $heredoc_string,
  87. $heredoc_numeric_string,
  88. $heredoc_empty_string
  89. );
  90. /* loop to check that strval() recognizes different
  91. scalar values and returns the string conversion of same */
  92. $loop_counter = 1;
  93. foreach ($scalars as $scalar ) {
  94. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  95. var_dump( strval($scalar) );
  96. }
  97. echo "\n*** Testing strval() with non_scalar values ***\n";
  98. // get a resource type variable
  99. $fp = fopen(__FILE__, "r");
  100. $dfp = opendir( __DIR__ );
  101. // unset variable
  102. $unset_var = 10;
  103. unset ($unset_var);
  104. // non_scalar values, objects, arrays, resources and boolean
  105. class foo
  106. {
  107. function __toString() {
  108. return "Object";
  109. }
  110. }
  111. $not_scalars = array (
  112. new foo, //object
  113. $fp, // resource
  114. $dfp,
  115. array(), // arrays
  116. array(NULL),
  117. array(1,2,3,4),
  118. array("string"),
  119. NULL, // nulls
  120. null,
  121. @$unset_var, // unset variable
  122. @$undefined_var
  123. );
  124. /* loop through the $not_scalars to see working of
  125. strval() on objects, arrays, boolean and others */
  126. $loop_counter = 1;
  127. foreach ($not_scalars as $value ) {
  128. echo "-- Iteration $loop_counter --\n"; $loop_counter++;
  129. var_dump( strval($value) );
  130. }
  131. echo "Done\n";
  132. // close the resources used
  133. fclose($fp);
  134. closedir($dfp);
  135. ?>
  136. --EXPECTF--
  137. *** Testing str_val() with scalar values***
  138. -- Iteration 1 --
  139. string(1) "0"
  140. -- Iteration 2 --
  141. string(1) "1"
  142. -- Iteration 3 --
  143. string(2) "-1"
  144. -- Iteration 4 --
  145. string(11) "-2147483648"
  146. -- Iteration 5 --
  147. string(11) "-2147483647"
  148. -- Iteration 6 --
  149. string(10) "2147483647"
  150. -- Iteration 7 --
  151. string(10) "2147483640"
  152. -- Iteration 8 --
  153. string(4) "4667"
  154. -- Iteration 9 --
  155. string(4) "4779"
  156. -- Iteration 10 --
  157. string(4) "4095"
  158. -- Iteration 11 --
  159. string(3) "250"
  160. -- Iteration 12 --
  161. string(11) "-2147483648"
  162. -- Iteration 13 --
  163. string(10) "2147483647"
  164. -- Iteration 14 --
  165. string(10) "2147483647"
  166. -- Iteration 15 --
  167. string(2) "83"
  168. -- Iteration 16 --
  169. string(1) "1"
  170. -- Iteration 17 --
  171. string(11) "-2147483648"
  172. -- Iteration 18 --
  173. string(10) "2147483647"
  174. -- Iteration 19 --
  175. string(11) "-2147483649"
  176. -- Iteration 20 --
  177. string(10) "2147483648"
  178. -- Iteration 21 --
  179. string(11) "-2147483649"
  180. -- Iteration 22 --
  181. string(11) "34359738369"
  182. -- Iteration 23 --
  183. string(10) "2147483649"
  184. -- Iteration 24 --
  185. string(11) "-2147483649"
  186. -- Iteration 25 --
  187. string(1) "0"
  188. -- Iteration 26 --
  189. string(4) "-0.1"
  190. -- Iteration 27 --
  191. string(2) "10"
  192. -- Iteration 28 --
  193. string(7) "1050000"
  194. -- Iteration 29 --
  195. string(6) "1.0E-5"
  196. -- Iteration 30 --
  197. string(7) "5000000"
  198. -- Iteration 31 --
  199. string(7) "6.0E-20"
  200. -- Iteration 32 --
  201. string(7) "5.0E+42"
  202. -- Iteration 33 --
  203. string(7) "3.4E-33"
  204. -- Iteration 34 --
  205. string(1) "1"
  206. -- Iteration 35 --
  207. string(1) "1"
  208. -- Iteration 36 --
  209. string(0) ""
  210. -- Iteration 37 --
  211. string(0) ""
  212. -- Iteration 38 --
  213. string(0) ""
  214. -- Iteration 39 --
  215. string(0) ""
  216. -- Iteration 40 --
  217. string(1) " "
  218. -- Iteration 41 --
  219. string(1) " "
  220. -- Iteration 42 --
  221. string(1) "0"
  222. -- Iteration 43 --
  223. string(1) "0"
  224. -- Iteration 44 --
  225. string(7) "testing"
  226. -- Iteration 45 --
  227. string(5) "0x564"
  228. -- Iteration 46 --
  229. string(4) "0123"
  230. -- Iteration 47 --
  231. string(4) "new
  232. "
  233. -- Iteration 48 --
  234. string(5) "new\n"
  235. -- Iteration 49 --
  236. string(11) "@#$$%^&&*()"
  237. -- Iteration 50 --
  238. string(8) " "
  239. -- Iteration 51 --
  240. string(4) "null"
  241. -- Iteration 52 --
  242. string(4) "null"
  243. -- Iteration 53 --
  244. string(4) "true"
  245. -- Iteration 54 --
  246. string(4) "true"
  247. -- Iteration 55 --
  248. string(5%d) "This is a multiline heredoc
  249. string. Numeric = 1232455."
  250. -- Iteration 56 --
  251. string(1%d) "12345
  252. 2345"
  253. -- Iteration 57 --
  254. string(0) ""
  255. *** Testing strval() with non_scalar values ***
  256. -- Iteration 1 --
  257. string(6) "Object"
  258. -- Iteration 2 --
  259. string(14) "Resource id #%d"
  260. -- Iteration 3 --
  261. string(14) "Resource id #%d"
  262. -- Iteration 4 --
  263. Warning: Array to string conversion in %s on line %d
  264. string(5) "Array"
  265. -- Iteration 5 --
  266. Warning: Array to string conversion in %s on line %d
  267. string(5) "Array"
  268. -- Iteration 6 --
  269. Warning: Array to string conversion in %s on line %d
  270. string(5) "Array"
  271. -- Iteration 7 --
  272. Warning: Array to string conversion in %s on line %d
  273. string(5) "Array"
  274. -- Iteration 8 --
  275. string(0) ""
  276. -- Iteration 9 --
  277. string(0) ""
  278. -- Iteration 10 --
  279. string(0) ""
  280. -- Iteration 11 --
  281. string(0) ""
  282. Done