printf.phpt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. --TEST--
  2. Test printf() function (32bit)
  3. --INI--
  4. precision=14
  5. --SKIPIF--
  6. <?php
  7. if (PHP_INT_MAX > 2147483647) {
  8. die("skip 32bit test only");
  9. }
  10. ?>
  11. --FILE--
  12. <?php
  13. /* Various input arrays for different format types */
  14. $float_variation = array( "%f", "%-f", "%+f", "%7.2f", "%-7.2f", "%07.2f", "%-07.2f", "%'#7.2f" );
  15. $float_numbers = array( 0, 1, -1, 0.32, -0.32, 3.4. -3.4, 2.54, -2.54, 1.2345678e99, -1.2345678e99 );
  16. $int_variation = array( "%d", "%-d", "%+d", "%7.2d", "%-7.2d", "%07.2d", "%-07.2d", "%'#7.2d" );
  17. $int_numbers = array( 0, 1, -1, 2.7, -2.7, 23333333, -23333333, "1234" );
  18. $char_variation = array( 'a', "a", 67, -67, 99 );
  19. $string_variation = array( "%5s", "%-5s", "%05s", "%'#5s" );
  20. $strings = array( NULL, "abc", 'aaa' );
  21. /* Checking warning messages */
  22. /* Zero argument */
  23. echo "\n*** Output for zero argument ***\n";
  24. printf();
  25. /* Number of arguments not matching as specified in format field */
  26. echo "\n*** Output for insufficient number of arguments ***\n";
  27. $string = "dingy%sflem%dwombat";
  28. $nbr = 5;
  29. $name = "voudras";
  30. printf("%d $string %s", $nbr, $name);
  31. /* Scalar argument */
  32. echo "\n*** Output for scalar argument ***\n";
  33. printf(3);
  34. /* NULL argument */
  35. echo "\n*** Output for NULL as argument ***\n";
  36. printf(NULL);
  37. /* Float type variations */
  38. $counter = 1;
  39. echo "\n\n*** Output for float type ***\n";
  40. echo "\n Input Float numbers variation array is:\n";
  41. print_r($float_numbers);
  42. foreach( $float_variation as $float_var )
  43. {
  44. echo "\n\nFloat Iteration $counter";
  45. foreach( $float_numbers as $float_num )
  46. {
  47. echo "\n";
  48. printf( $float_var, $float_num );
  49. }
  50. $counter++;
  51. }
  52. /* Integer type variations */
  53. $counter = 1;
  54. echo "\n\n*** Output for integer type ***\n";
  55. echo "\n Input Integer numbers variation array is:\n";
  56. print_r($int_numbers);
  57. foreach( $int_variation as $int_var )
  58. {
  59. echo "\n\nInteger Iteration $counter";
  60. foreach( $int_numbers as $int_num )
  61. {
  62. echo "\n";
  63. printf( $int_var, $int_num );
  64. }
  65. $counter++;
  66. }
  67. /* Binary type variations */
  68. echo "\n\n*** Output for binary type ***\n";
  69. echo "\n Input numbers variation array is:\n";
  70. print_r($int_numbers);
  71. foreach( $int_numbers as $bin_num )
  72. {
  73. echo "\n";
  74. printf( "%b", $bin_num );
  75. }
  76. /* Chararter type variations */
  77. echo "\n\n*** Output for char type ***\n";
  78. echo "\n Input Characters variation array is:\n";
  79. print_r($char_variation);
  80. foreach( $char_variation as $char )
  81. {
  82. echo "\n";
  83. printf( "%c", $char );
  84. }
  85. /* Scientific type variations */
  86. echo "\n\n*** Output for scientific type ***\n";
  87. echo "\n Input numbers variation array is:\n";
  88. print_r($int_numbers);
  89. foreach( $int_numbers as $num )
  90. {
  91. echo "\n";
  92. printf( "%e", $num );
  93. }
  94. /* Unsigned Integer type variation */
  95. echo "\n\n*** Output for unsigned integer type ***\n";
  96. echo "\n Input Integer numbers variation array is:\n";
  97. print_r($int_numbers);
  98. foreach( $int_numbers as $unsig_num )
  99. {
  100. echo "\n";
  101. printf( "%u", $unsig_num );
  102. }
  103. /* Octal type variations */
  104. echo "\n\n*** Output for octal type ***\n";
  105. echo "\n Input numbers variation array is:\n";
  106. print_r($int_numbers);
  107. foreach( $int_numbers as $octal_num )
  108. {
  109. echo "\n";
  110. printf( "%o", $octal_num );
  111. }
  112. /* Hexadecimal type variations */
  113. echo "\n\n*** Output for hexadecimal type ***\n";
  114. echo "\n Input numbers variation array is:\n";
  115. print_r($int_numbers);
  116. foreach( $int_numbers as $hexa_num )
  117. {
  118. echo "\n";
  119. printf( "%x", $hexa_num );
  120. }
  121. /* String type variations */
  122. echo "\n\n*** Output for string type ***\n";
  123. echo "\n Input Strings format variation array is:\n";
  124. print_r($string_variation);
  125. echo "\n Input strings variation array is:\n";
  126. print_r($strings);
  127. foreach( $string_variation as $string_var )
  128. {
  129. foreach( $strings as $str )
  130. {
  131. echo "\n";
  132. printf( $string_var, $str );
  133. }
  134. }
  135. /* variations of %g type */
  136. $format_g = array("%g", "%.0g", "%+g", "%-g", "%-1.2g", "%+1.2g", "%G", "%.0G", "%+G", "%-G", "%-1.2G", "%+1.2G");
  137. echo "\n\n*** Output for '%g' type ***\n";
  138. echo "\n Input format variation array is:\n";
  139. print_r($format_g);
  140. foreach( $format_g as $formatg )
  141. {
  142. printf("\n$formatg",123456);
  143. printf("\n$formatg",-123456);
  144. }
  145. /* Some more typical cases */
  146. $tempnum = 12345;
  147. $tempstring = "abcdefghjklmnpqrstuvwxyz";
  148. echo"\n\n*** Output for '%%%.2f' as the format parameter ***\n";
  149. printf("%%%.2f",1.23456789e10);
  150. echo"\n\n*** Output for '%%' as the format parameter ***\n";
  151. printf("%%",1.23456789e10);
  152. echo"\n\n*** Output for precision value more than maximum ***\n";
  153. printf("%.988f",1.23456789e10);
  154. echo"\n\n*** Output for invalid width(-15) specifier ***\n";
  155. printf("%030.-15s", $tempstring);
  156. echo"\n\n*** Output for '%F' as the format parameter ***\n";
  157. printf("%F",1.23456789e10);
  158. echo"\n\n*** Output for '%X' as the format parameter ***\n";
  159. printf("%X",12);
  160. echo"\n\n*** Output with no format parameter ***\n";
  161. printf($tempnum);
  162. echo"\n\n*** Output for multiple format parameters ***\n";
  163. printf("%d %s %d\n", $tempnum, $tempstring, $tempnum);
  164. echo"\n\n*** Output for excess of mixed type arguments ***\n";
  165. printf("%s", $tempstring, $tempstring, $tempstring);
  166. echo"\n\n*** Output for string format parameter and integer type argument ***\n";
  167. printf("%s", $tempnum);
  168. echo"\n\n*** Output for integer format parameter and string type argument ***\n";
  169. printf("%d", $tempstring);
  170. ?>
  171. --EXPECTF--
  172. *** Output for zero argument ***
  173. Warning: printf() expects at least %d parameter, %d given in %s on line %d
  174. *** Output for insufficient number of arguments ***
  175. Warning: printf(): Too few arguments in %s on line %d
  176. *** Output for scalar argument ***
  177. 3
  178. *** Output for NULL as argument ***
  179. *** Output for float type ***
  180. Input Float numbers variation array is:
  181. Array
  182. (
  183. [0] => 0
  184. [1] => 1
  185. [2] => -1
  186. [3] => 0.32
  187. [4] => -0.32
  188. [5] => 3.4-3.4
  189. [6] => 2.54
  190. [7] => -2.54
  191. [8] => 1.2345678E+99
  192. [9] => -1.2345678E+99
  193. )
  194. Float Iteration 1
  195. 0.000000
  196. 1.000000
  197. -1.000000
  198. 0.320000
  199. -0.320000
  200. 3.400000
  201. 2.540000
  202. -2.540000
  203. 1234567%d.000000
  204. -1234567%d.000000
  205. Float Iteration 2
  206. 0.000000
  207. 1.000000
  208. -1.000000
  209. 0.320000
  210. -0.320000
  211. 3.400000
  212. 2.540000
  213. -2.540000
  214. 1234567%d.000000
  215. -1234567%d.000000
  216. Float Iteration 3
  217. +0.000000
  218. +1.000000
  219. -1.000000
  220. +0.320000
  221. -0.320000
  222. +3.400000
  223. +2.540000
  224. -2.540000
  225. +1234567%d.000000
  226. -1234567%d.000000
  227. Float Iteration 4
  228. 0.00
  229. 1.00
  230. -1.00
  231. 0.32
  232. -0.32
  233. 3.40
  234. 2.54
  235. -2.54
  236. 1234567%d.00
  237. -1234567%d.00
  238. Float Iteration 5
  239. 0.00
  240. 1.00
  241. -1.00
  242. 0.32
  243. -0.32
  244. 3.40
  245. 2.54
  246. -2.54
  247. 1234567%d.00
  248. -1234567%d.00
  249. Float Iteration 6
  250. 0000.00
  251. 0001.00
  252. -001.00
  253. 0000.32
  254. -000.32
  255. 0003.40
  256. 0002.54
  257. -002.54
  258. 1234567%d.00
  259. -1234567%d.00
  260. Float Iteration 7
  261. 0.00000
  262. 1.00000
  263. -1.0000
  264. 0.32000
  265. -0.3200
  266. 3.40000
  267. 2.54000
  268. -2.5400
  269. 1234567%d.00
  270. -1234567%d.00
  271. Float Iteration 8
  272. ###0.00
  273. ###1.00
  274. ##-1.00
  275. ###0.32
  276. ##-0.32
  277. ###3.40
  278. ###2.54
  279. ##-2.54
  280. 1234567%d.00
  281. -1234567%d.00
  282. *** Output for integer type ***
  283. Input Integer numbers variation array is:
  284. Array
  285. (
  286. [0] => 0
  287. [1] => 1
  288. [2] => -1
  289. [3] => 2.7
  290. [4] => -2.7
  291. [5] => 23333333
  292. [6] => -23333333
  293. [7] => 1234
  294. )
  295. Integer Iteration 1
  296. 0
  297. 1
  298. -1
  299. 2
  300. -2
  301. 23333333
  302. -23333333
  303. 1234
  304. Integer Iteration 2
  305. 0
  306. 1
  307. -1
  308. 2
  309. -2
  310. 23333333
  311. -23333333
  312. 1234
  313. Integer Iteration 3
  314. +0
  315. +1
  316. -1
  317. +2
  318. -2
  319. +23333333
  320. -23333333
  321. +1234
  322. Integer Iteration 4
  323. 0
  324. 1
  325. -1
  326. 2
  327. -2
  328. 23333333
  329. -23333333
  330. 1234
  331. Integer Iteration 5
  332. 0
  333. 1
  334. -1
  335. 2
  336. -2
  337. 23333333
  338. -23333333
  339. 1234
  340. Integer Iteration 6
  341. 0000000
  342. 0000001
  343. -000001
  344. 0000002
  345. -000002
  346. 23333333
  347. -23333333
  348. 0001234
  349. Integer Iteration 7
  350. 0
  351. 1
  352. -1
  353. 2
  354. -2
  355. 23333333
  356. -23333333
  357. 1234
  358. Integer Iteration 8
  359. ######0
  360. ######1
  361. #####-1
  362. ######2
  363. #####-2
  364. 23333333
  365. -23333333
  366. ###1234
  367. *** Output for binary type ***
  368. Input numbers variation array is:
  369. Array
  370. (
  371. [0] => 0
  372. [1] => 1
  373. [2] => -1
  374. [3] => 2.7
  375. [4] => -2.7
  376. [5] => 23333333
  377. [6] => -23333333
  378. [7] => 1234
  379. )
  380. 0
  381. 1
  382. 11111111111111111111111111111111
  383. 10
  384. 11111111111111111111111111111110
  385. 1011001000000100111010101
  386. 11111110100110111111011000101011
  387. 10011010010
  388. *** Output for char type ***
  389. Input Characters variation array is:
  390. Array
  391. (
  392. [0] => a
  393. [1] => a
  394. [2] => 67
  395. [3] => -67
  396. [4] => 99
  397. )
  398. C
  399. ½
  400. c
  401. *** Output for scientific type ***
  402. Input numbers variation array is:
  403. Array
  404. (
  405. [0] => 0
  406. [1] => 1
  407. [2] => -1
  408. [3] => 2.7
  409. [4] => -2.7
  410. [5] => 23333333
  411. [6] => -23333333
  412. [7] => 1234
  413. )
  414. 0.000000e+0
  415. 1.000000e+0
  416. -1.000000e+0
  417. 2.700000e+0
  418. -2.700000e+0
  419. 2.333333e+7
  420. -2.333333e+7
  421. 1.234000e+3
  422. *** Output for unsigned integer type ***
  423. Input Integer numbers variation array is:
  424. Array
  425. (
  426. [0] => 0
  427. [1] => 1
  428. [2] => -1
  429. [3] => 2.7
  430. [4] => -2.7
  431. [5] => 23333333
  432. [6] => -23333333
  433. [7] => 1234
  434. )
  435. 0
  436. 1
  437. 4294967295
  438. 2
  439. 4294967294
  440. 23333333
  441. 4271633963
  442. 1234
  443. *** Output for octal type ***
  444. Input numbers variation array is:
  445. Array
  446. (
  447. [0] => 0
  448. [1] => 1
  449. [2] => -1
  450. [3] => 2.7
  451. [4] => -2.7
  452. [5] => 23333333
  453. [6] => -23333333
  454. [7] => 1234
  455. )
  456. 0
  457. 1
  458. 37777777777
  459. 2
  460. 37777777776
  461. 131004725
  462. 37646773053
  463. 2322
  464. *** Output for hexadecimal type ***
  465. Input numbers variation array is:
  466. Array
  467. (
  468. [0] => 0
  469. [1] => 1
  470. [2] => -1
  471. [3] => 2.7
  472. [4] => -2.7
  473. [5] => 23333333
  474. [6] => -23333333
  475. [7] => 1234
  476. )
  477. 0
  478. 1
  479. ffffffff
  480. 2
  481. fffffffe
  482. 16409d5
  483. fe9bf62b
  484. 4d2
  485. *** Output for string type ***
  486. Input Strings format variation array is:
  487. Array
  488. (
  489. [0] => %5s
  490. [1] => %-5s
  491. [2] => %05s
  492. [3] => %'#5s
  493. )
  494. Input strings variation array is:
  495. Array
  496. (
  497. [0] =>
  498. [1] => abc
  499. [2] => aaa
  500. )
  501. abc
  502. aaa
  503. abc
  504. aaa
  505. 00000
  506. 00abc
  507. 00aaa
  508. #####
  509. ##abc
  510. ##aaa
  511. *** Output for '%g' type ***
  512. Input format variation array is:
  513. Array
  514. (
  515. [0] => %g
  516. [1] => %.0g
  517. [2] => %+g
  518. [3] => %-g
  519. [4] => %-1.2g
  520. [5] => %+1.2g
  521. [6] => %G
  522. [7] => %.0G
  523. [8] => %+G
  524. [9] => %-G
  525. [10] => %-1.2G
  526. [11] => %+1.2G
  527. )
  528. 123456
  529. -123456
  530. 1.0e+5
  531. -1.0e+5
  532. +123456
  533. -123456
  534. 123456
  535. -123456
  536. 1.2e+5
  537. -1.2e+5
  538. +1.2e+5
  539. -1.2e+5
  540. 123456
  541. -123456
  542. 1.0E+5
  543. -1.0E+5
  544. +123456
  545. -123456
  546. 123456
  547. -123456
  548. 1.2E+5
  549. -1.2E+5
  550. +1.2E+5
  551. -1.2E+5
  552. *** Output for '%%%.2f' as the format parameter ***
  553. %12345678900.00
  554. *** Output for '%%' as the format parameter ***
  555. %
  556. *** Output for precision value more than maximum ***
  557. Notice: printf(): Requested precision of 988 digits was truncated to PHP maximum of 53 digits in %s on line %d
  558. 12345678900.00000000000000000000000000000000000000000000000000000
  559. *** Output for invalid width(-15) specifier ***
  560. 15s
  561. *** Output for '%F' as the format parameter ***
  562. 12345678900.000000
  563. *** Output for '%X' as the format parameter ***
  564. C
  565. *** Output with no format parameter ***
  566. 12345
  567. *** Output for multiple format parameters ***
  568. 12345 abcdefghjklmnpqrstuvwxyz 12345
  569. *** Output for excess of mixed type arguments ***
  570. abcdefghjklmnpqrstuvwxyz
  571. *** Output for string format parameter and integer type argument ***
  572. 12345
  573. *** Output for integer format parameter and string type argument ***
  574. 0