1--TEST-- 2Test printf() function (64bit) 3--SKIPIF-- 4<?php 5if (!(PHP_INT_MAX > 2147483647)) { 6 die("skip 64bit test only"); 7} 8?> 9--INI-- 10precision=14 11--FILE-- 12<?php 13 14/* Various input arrays for different format types */ 15 16$float_variation = array( "%f", "%-f", "%+f", "%7.2f", "%-7.2f", "%07.2f", "%-07.2f", "%'#7.2f" ); 17$float_numbers = array( 0, 1, -1, 0.32, -0.32, 3.4. -3.4, 2.54, -2.54, 1.2345678e99, -1.2345678e99 ); 18 19$int_variation = array( "%d", "%-d", "%+d", "%7.2d", "%-7.2d", "%07.2d", "%-07.2d", "%'#7.2d" ); 20$int_numbers = array( 0, 1, -1, 2.7, -2.7, 23333333, -23333333, "1234" ); 21 22$char_variation = array( 'a', "a", 67, -67, 99 ); 23 24$string_variation = array( "%5s", "%-5s", "%05s", "%'#5s" ); 25$strings = array( NULL, "abc", 'aaa' ); 26 27/* Checking warning messages */ 28 29/* Zero argument */ 30echo "\n*** Output for zero argument ***\n"; 31try { 32 printf(); 33} catch (TypeError $e) { 34 echo $e->getMessage(), "\n"; 35} 36 37/* Number of arguments not matching as specified in format field */ 38echo "\n*** Output for insufficient number of arguments ***\n"; 39$string = "dingy%sflem%dwombat"; 40$nbr = 5; 41$name = "voudras"; 42try { 43 printf("%d $string %s", $nbr, $name); 44} catch (\ArgumentCountError $e) { 45 print('Error found: '.$e->getMessage()); 46} 47 48 49/* Scalar argument */ 50echo "\n*** Output for scalar argument ***\n"; 51printf(3); 52 53/* NULL argument */ 54echo "\n*** Output for NULL as argument ***\n"; 55printf(NULL); 56 57 58/* Float type variations */ 59 60$counter = 1; 61echo "\n\n*** Output for float type ***\n"; 62echo "\n Input Float numbers variation array is:\n"; 63print_r($float_numbers); 64 65foreach( $float_variation as $float_var ) 66{ 67 echo "\n\nFloat Iteration $counter"; 68 foreach( $float_numbers as $float_num ) 69 { 70 echo "\n"; 71 printf( $float_var, $float_num ); 72 } 73 $counter++; 74} 75 76 77/* Integer type variations */ 78 79$counter = 1; 80echo "\n\n*** Output for integer type ***\n"; 81echo "\n Input Integer numbers variation array is:\n"; 82print_r($int_numbers); 83 84foreach( $int_variation as $int_var ) 85{ 86 echo "\n\nInteger Iteration $counter"; 87 foreach( $int_numbers as $int_num ) 88 { 89 echo "\n"; 90 printf( $int_var, $int_num ); 91 } 92 $counter++; 93} 94 95 96/* Binary type variations */ 97 98echo "\n\n*** Output for binary type ***\n"; 99echo "\n Input numbers variation array is:\n"; 100print_r($int_numbers); 101 102 foreach( $int_numbers as $bin_num ) 103 { 104 echo "\n"; 105 printf( "%b", $bin_num ); 106 } 107 108 109/* Chararter type variations */ 110echo "\n\n*** Output for char type ***\n"; 111echo "\n Input Characters variation array is:\n"; 112print_r($char_variation); 113 114foreach( $char_variation as $char ) 115{ 116 echo "\n"; 117 printf( "%c", $char ); 118} 119 120/* Scientific type variations */ 121echo "\n\n*** Output for scientific type ***\n"; 122echo "\n Input numbers variation array is:\n"; 123print_r($int_numbers); 124 125foreach( $int_numbers as $num ) 126{ 127 echo "\n"; 128 printf( "%e", $num ); 129} 130 131/* Unsigned Integer type variation */ 132echo "\n\n*** Output for unsigned integer type ***\n"; 133echo "\n Input Integer numbers variation array is:\n"; 134print_r($int_numbers); 135 136foreach( $int_numbers as $unsig_num ) 137{ 138 echo "\n"; 139 printf( "%u", $unsig_num ); 140} 141 142/* Octal type variations */ 143echo "\n\n*** Output for octal type ***\n"; 144echo "\n Input numbers variation array is:\n"; 145print_r($int_numbers); 146 147foreach( $int_numbers as $octal_num ) 148{ 149 echo "\n"; 150 printf( "%o", $octal_num ); 151} 152 153/* Hexadecimal type variations */ 154echo "\n\n*** Output for hexadecimal type ***\n"; 155echo "\n Input numbers variation array is:\n"; 156print_r($int_numbers); 157 158foreach( $int_numbers as $hexa_num ) 159{ 160 echo "\n"; 161 printf( "%x", $hexa_num ); 162} 163 164/* String type variations */ 165echo "\n\n*** Output for string type ***\n"; 166echo "\n Input Strings format variation array is:\n"; 167print_r($string_variation); 168echo "\n Input strings variation array is:\n"; 169print_r($strings); 170 171foreach( $string_variation as $string_var ) 172{ 173 foreach( $strings as $str ) 174 { 175 echo "\n"; 176 printf( $string_var, $str ); 177 } 178} 179 180 181/* variations of %g type */ 182$format_g = array("%g", "%.0g", "%+g", "%-g", "%-1.2g", "%+1.2g", "%G", "%.0G", "%+G", "%-G", "%-1.2G", "%+1.2G"); 183 184echo "\n\n*** Output for '%g' type ***\n"; 185echo "\n Input format variation array is:\n"; 186print_r($format_g); 187 188foreach( $format_g as $formatg ) 189{ 190 printf("\n$formatg",123456); 191 printf("\n$formatg",-123456); 192} 193 194 195/* Some more typical cases */ 196 197$tempnum = 12345; 198$tempstring = "abcdefghjklmnpqrstuvwxyz"; 199 200echo"\n\n*** Output for '%%%.2f' as the format parameter ***\n"; 201printf("%%%.2f",1.23456789e10); 202 203echo"\n\n*** Output for '%%' as the format parameter ***\n"; 204printf("%%",1.23456789e10); 205 206echo"\n\n*** Output for precision value more than maximum ***\n"; 207printf("%.988f",1.23456789e10); 208 209echo"\n\n*** Output for invalid width(-15) specifier ***\n"; 210try { 211 printf("%030.-15s", $tempstring); 212} catch (ValueError $e) { 213 echo $e->getMessage(); 214} 215 216echo"\n\n*** Output for '%F' as the format parameter ***\n"; 217printf("%F",1.23456789e10); 218 219echo"\n\n*** Output for '%X' as the format parameter ***\n"; 220printf("%X",12); 221 222echo"\n\n*** Output with no format parameter ***\n"; 223printf($tempnum); 224 225echo"\n\n*** Output for multiple format parameters ***\n"; 226printf("%d %s %d\n", $tempnum, $tempstring, $tempnum); 227 228echo"\n\n*** Output for excess of mixed type arguments ***\n"; 229printf("%s", $tempstring, $tempstring, $tempstring); 230 231echo"\n\n*** Output for string format parameter and integer type argument ***\n"; 232printf("%s", $tempnum); 233 234echo"\n\n*** Output for integer format parameter and string type argument ***\n"; 235printf("%d", $tempstring); 236 237 238?> 239--EXPECTF-- 240*** Output for zero argument *** 241printf() expects at least 1 argument, 0 given 242 243*** Output for insufficient number of arguments *** 244Error found: 5 arguments are required, 3 given 245*** Output for scalar argument *** 2463 247*** Output for NULL as argument *** 248 249 250*** Output for float type *** 251 252 Input Float numbers variation array is: 253Array 254( 255 [0] => 0 256 [1] => 1 257 [2] => -1 258 [3] => 0.32 259 [4] => -0.32 260 [5] => 3.4-3.4 261 [6] => 2.54 262 [7] => -2.54 263 [8] => 1.2345678E+99 264 [9] => -1.2345678E+99 265) 266 267 268Float Iteration 1 2690.000000 2701.000000 271-1.000000 2720.320000 273-0.320000 2743.400000 2752.540000 276-2.540000 2771234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.000000 278-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.000000 279 280Float Iteration 2 2810.000000 2821.000000 283-1.000000 2840.320000 285-0.320000 2863.400000 2872.540000 288-2.540000 2891234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.000000 290-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.000000 291 292Float Iteration 3 293+0.000000 294+1.000000 295-1.000000 296+0.320000 297-0.320000 298+3.400000 299+2.540000 300-2.540000 301+1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.000000 302-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.000000 303 304Float Iteration 4 305 0.00 306 1.00 307 -1.00 308 0.32 309 -0.32 310 3.40 311 2.54 312 -2.54 3131234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 314-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 315 316Float Iteration 5 3170.00 3181.00 319-1.00 3200.32 321-0.32 3223.40 3232.54 324-2.54 3251234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 326-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 327 328Float Iteration 6 3290000.00 3300001.00 331-001.00 3320000.32 333-000.32 3340003.40 3350002.54 336-002.54 3371234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 338-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 339 340Float Iteration 7 3410.00000 3421.00000 343-1.0000 3440.32000 345-0.3200 3463.40000 3472.54000 348-2.5400 3491234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 350-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 351 352Float Iteration 8 353###0.00 354###1.00 355##-1.00 356###0.32 357##-0.32 358###3.40 359###2.54 360##-2.54 3611234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 362-1234567799999999974395005011934353453808157274826416694779123657996479008398428960266851879135215616.00 363 364*** Output for integer type *** 365 366 Input Integer numbers variation array is: 367Array 368( 369 [0] => 0 370 [1] => 1 371 [2] => -1 372 [3] => 2.7 373 [4] => -2.7 374 [5] => 23333333 375 [6] => -23333333 376 [7] => 1234 377) 378 379 380Integer Iteration 1 3810 3821 383-1 3842 385-2 38623333333 387-23333333 3881234 389 390Integer Iteration 2 3910 3921 393-1 3942 395-2 39623333333 397-23333333 3981234 399 400Integer Iteration 3 401+0 402+1 403-1 404+2 405-2 406+23333333 407-23333333 408+1234 409 410Integer Iteration 4 411 0 412 1 413 -1 414 2 415 -2 41623333333 417-23333333 418 1234 419 420Integer Iteration 5 4210 4221 423-1 4242 425-2 42623333333 427-23333333 4281234 429 430Integer Iteration 6 4310000000 4320000001 433-000001 4340000002 435-000002 43623333333 437-23333333 4380001234 439 440Integer Iteration 7 4410 4421 443-1 4442 445-2 44623333333 447-23333333 4481234 449 450Integer Iteration 8 451######0 452######1 453#####-1 454######2 455#####-2 45623333333 457-23333333 458###1234 459 460*** Output for binary type *** 461 462 Input numbers variation array is: 463Array 464( 465 [0] => 0 466 [1] => 1 467 [2] => -1 468 [3] => 2.7 469 [4] => -2.7 470 [5] => 23333333 471 [6] => -23333333 472 [7] => 1234 473) 474 4750 4761 4771111111111111111111111111111111111111111111111111111111111111111 47810 4791111111111111111111111111111111111111111111111111111111111111110 4801011001000000100111010101 4811111111111111111111111111111111111111110100110111111011000101011 48210011010010 483 484*** Output for char type *** 485 486 Input Characters variation array is: 487Array 488( 489 [0] => a 490 [1] => a 491 [2] => 67 492 [3] => -67 493 [4] => 99 494) 495 496 497 498C 499� 500c 501 502*** Output for scientific type *** 503 504 Input numbers variation array is: 505Array 506( 507 [0] => 0 508 [1] => 1 509 [2] => -1 510 [3] => 2.7 511 [4] => -2.7 512 [5] => 23333333 513 [6] => -23333333 514 [7] => 1234 515) 516 5170.000000e+0 5181.000000e+0 519-1.000000e+0 5202.700000e+0 521-2.700000e+0 5222.333333e+7 523-2.333333e+7 5241.234000e+3 525 526*** Output for unsigned integer type *** 527 528 Input Integer numbers variation array is: 529Array 530( 531 [0] => 0 532 [1] => 1 533 [2] => -1 534 [3] => 2.7 535 [4] => -2.7 536 [5] => 23333333 537 [6] => -23333333 538 [7] => 1234 539) 540 5410 5421 54318446744073709551615 5442 54518446744073709551614 54623333333 54718446744073686218283 5481234 549 550*** Output for octal type *** 551 552 Input numbers variation array is: 553Array 554( 555 [0] => 0 556 [1] => 1 557 [2] => -1 558 [3] => 2.7 559 [4] => -2.7 560 [5] => 23333333 561 [6] => -23333333 562 [7] => 1234 563) 564 5650 5661 5671777777777777777777777 5682 5691777777777777777777776 570131004725 5711777777777777646773053 5722322 573 574*** Output for hexadecimal type *** 575 576 Input numbers variation array is: 577Array 578( 579 [0] => 0 580 [1] => 1 581 [2] => -1 582 [3] => 2.7 583 [4] => -2.7 584 [5] => 23333333 585 [6] => -23333333 586 [7] => 1234 587) 588 5890 5901 591ffffffffffffffff 5922 593fffffffffffffffe 59416409d5 595fffffffffe9bf62b 5964d2 597 598*** Output for string type *** 599 600 Input Strings format variation array is: 601Array 602( 603 [0] => %5s 604 [1] => %-5s 605 [2] => %05s 606 [3] => %'#5s 607) 608 609 Input strings variation array is: 610Array 611( 612 [0] => 613 [1] => abc 614 [2] => aaa 615) 616 617 618 abc 619 aaa 620 621abc 622aaa 62300000 62400abc 62500aaa 626##### 627##abc 628##aaa 629 630*** Output for '%g' type *** 631 632 Input format variation array is: 633Array 634( 635 [0] => %g 636 [1] => %.0g 637 [2] => %+g 638 [3] => %-g 639 [4] => %-1.2g 640 [5] => %+1.2g 641 [6] => %G 642 [7] => %.0G 643 [8] => %+G 644 [9] => %-G 645 [10] => %-1.2G 646 [11] => %+1.2G 647) 648 649123456 650-123456 6511.0e+5 652-1.0e+5 653+123456 654-123456 655123456 656-123456 6571.2e+5 658-1.2e+5 659+1.2e+5 660-1.2e+5 661123456 662-123456 6631.0E+5 664-1.0E+5 665+123456 666-123456 667123456 668-123456 6691.2E+5 670-1.2E+5 671+1.2E+5 672-1.2E+5 673 674*** Output for '%%%.2f' as the format parameter *** 675%12345678900.00 676 677*** Output for '%%' as the format parameter *** 678% 679 680*** Output for precision value more than maximum *** 681 682Notice: printf(): Requested precision of 988 digits was truncated to PHP maximum of %d digits in %s on line %d 68312345678900.0000000000%d 684 685*** Output for invalid width(-15) specifier *** 686Unknown format specifier "-" 687 688*** Output for '%F' as the format parameter *** 68912345678900.000000 690 691*** Output for '%X' as the format parameter *** 692C 693 694*** Output with no format parameter *** 69512345 696 697*** Output for multiple format parameters *** 69812345 abcdefghjklmnpqrstuvwxyz 12345 699 700 701*** Output for excess of mixed type arguments *** 702abcdefghjklmnpqrstuvwxyz 703 704*** Output for string format parameter and integer type argument *** 70512345 706 707*** Output for integer format parameter and string type argument *** 7080 709