printf.phpt 11 KB

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