1--TEST-- 2Test fscanf() function: usage variations - integer formats with integer values 3--FILE-- 4<?php 5 6/* Test fscanf() to scan different integer values using different integer format types */ 7 8$file_path = __DIR__; 9 10echo "*** Test fscanf(): different integer format types with different integer values ***\n"; 11 12// create a file 13$filename = "$file_path/fscanf_variation2.tmp"; 14$file_handle = fopen($filename, "w"); 15if($file_handle == false) 16 exit("Error:failed to open file $filename"); 17 18// different valid integer values 19$valid_ints = array( 20 0, 21 1, 22 -1, 23 -2147483648, // max negative integer value 24 -2147483647, 25 2147483647, // max positive integer value 26 2147483640, 27 0x123B, // integer as hexadecimal 28 0x12ab, 29 0Xfff, 30 0XFA, 31 -0x80000000, // max negative integer as hexadecimal 32 0x7fffffff, // max positive integer as hexadecimal 33 0x7FFFFFFF, // max positive integer as hexadecimal 34 0123, // integer as octal 35 01, // should be quivalent to octal 1 36 -020000000000, // max negative integer as octal 37 017777777777 // max positive integer as octal 38); 39// various integer formats 40$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d"); 41 42$counter = 1; 43 44// writing to the file 45foreach($valid_ints as $int_value) { 46 @fprintf($file_handle, $int_value); 47 @fprintf($file_handle, "\n"); 48} 49// closing the file 50fclose($file_handle); 51 52// opening the file for reading 53$file_handle = fopen($filename, "r"); 54if($file_handle == false) { 55 exit("Error:failed to open file $filename"); 56} 57 58$counter = 1; 59// reading the values from file using different integer formats 60foreach($int_formats as $int_format) { 61 // rewind the file so that for every foreach iteration the file pointer starts from bof 62 rewind($file_handle); 63 echo "\n-- iteration $counter --\n"; 64 while( !feof($file_handle) ) { 65 try { 66 var_dump(fscanf($file_handle,$int_format)); 67 } catch (ValueError $exception) { 68 echo $exception->getMessage() . "\n"; 69 } 70 } 71 $counter++; 72} 73 74echo "\n*** Done ***"; 75?> 76--CLEAN-- 77<?php 78$file_path = __DIR__; 79$filename = "$file_path/fscanf_variation2.tmp"; 80unlink($filename); 81?> 82--EXPECT-- 83*** Test fscanf(): different integer format types with different integer values *** 84 85-- iteration 1 -- 86array(1) { 87 [0]=> 88 int(0) 89} 90array(1) { 91 [0]=> 92 int(1) 93} 94array(1) { 95 [0]=> 96 int(-1) 97} 98array(1) { 99 [0]=> 100 int(-2147483648) 101} 102array(1) { 103 [0]=> 104 int(-2147483647) 105} 106array(1) { 107 [0]=> 108 int(2147483647) 109} 110array(1) { 111 [0]=> 112 int(2147483640) 113} 114array(1) { 115 [0]=> 116 int(4667) 117} 118array(1) { 119 [0]=> 120 int(4779) 121} 122array(1) { 123 [0]=> 124 int(4095) 125} 126array(1) { 127 [0]=> 128 int(250) 129} 130array(1) { 131 [0]=> 132 int(-2147483648) 133} 134array(1) { 135 [0]=> 136 int(2147483647) 137} 138array(1) { 139 [0]=> 140 int(2147483647) 141} 142array(1) { 143 [0]=> 144 int(83) 145} 146array(1) { 147 [0]=> 148 int(1) 149} 150array(1) { 151 [0]=> 152 int(-2147483648) 153} 154array(1) { 155 [0]=> 156 int(2147483647) 157} 158bool(false) 159 160-- iteration 2 -- 161array(1) { 162 [0]=> 163 int(0) 164} 165array(1) { 166 [0]=> 167 int(1) 168} 169array(1) { 170 [0]=> 171 int(-1) 172} 173array(1) { 174 [0]=> 175 int(-2147483648) 176} 177array(1) { 178 [0]=> 179 int(-2147483647) 180} 181array(1) { 182 [0]=> 183 int(2147483647) 184} 185array(1) { 186 [0]=> 187 int(2147483640) 188} 189array(1) { 190 [0]=> 191 int(4667) 192} 193array(1) { 194 [0]=> 195 int(4779) 196} 197array(1) { 198 [0]=> 199 int(4095) 200} 201array(1) { 202 [0]=> 203 int(250) 204} 205array(1) { 206 [0]=> 207 int(-2147483648) 208} 209array(1) { 210 [0]=> 211 int(2147483647) 212} 213array(1) { 214 [0]=> 215 int(2147483647) 216} 217array(1) { 218 [0]=> 219 int(83) 220} 221array(1) { 222 [0]=> 223 int(1) 224} 225array(1) { 226 [0]=> 227 int(-2147483648) 228} 229array(1) { 230 [0]=> 231 int(2147483647) 232} 233bool(false) 234 235-- iteration 3 -- 236array(1) { 237 [0]=> 238 int(0) 239} 240array(1) { 241 [0]=> 242 int(1) 243} 244array(1) { 245 [0]=> 246 int(-1) 247} 248array(1) { 249 [0]=> 250 int(-2147483648) 251} 252array(1) { 253 [0]=> 254 int(-2147483647) 255} 256array(1) { 257 [0]=> 258 int(2147483647) 259} 260array(1) { 261 [0]=> 262 int(2147483640) 263} 264array(1) { 265 [0]=> 266 int(4667) 267} 268array(1) { 269 [0]=> 270 int(4779) 271} 272array(1) { 273 [0]=> 274 int(4095) 275} 276array(1) { 277 [0]=> 278 int(250) 279} 280array(1) { 281 [0]=> 282 int(-2147483648) 283} 284array(1) { 285 [0]=> 286 int(2147483647) 287} 288array(1) { 289 [0]=> 290 int(2147483647) 291} 292array(1) { 293 [0]=> 294 int(83) 295} 296array(1) { 297 [0]=> 298 int(1) 299} 300array(1) { 301 [0]=> 302 int(-2147483648) 303} 304array(1) { 305 [0]=> 306 int(2147483647) 307} 308bool(false) 309 310-- iteration 4 -- 311array(1) { 312 [0]=> 313 int(0) 314} 315array(1) { 316 [0]=> 317 int(1) 318} 319array(1) { 320 [0]=> 321 int(-1) 322} 323array(1) { 324 [0]=> 325 int(-2147483648) 326} 327array(1) { 328 [0]=> 329 int(-2147483647) 330} 331array(1) { 332 [0]=> 333 int(2147483647) 334} 335array(1) { 336 [0]=> 337 int(2147483640) 338} 339array(1) { 340 [0]=> 341 int(4667) 342} 343array(1) { 344 [0]=> 345 int(4779) 346} 347array(1) { 348 [0]=> 349 int(4095) 350} 351array(1) { 352 [0]=> 353 int(250) 354} 355array(1) { 356 [0]=> 357 int(-2147483648) 358} 359array(1) { 360 [0]=> 361 int(2147483647) 362} 363array(1) { 364 [0]=> 365 int(2147483647) 366} 367array(1) { 368 [0]=> 369 int(83) 370} 371array(1) { 372 [0]=> 373 int(1) 374} 375array(1) { 376 [0]=> 377 int(-2147483648) 378} 379array(1) { 380 [0]=> 381 int(2147483647) 382} 383bool(false) 384 385-- iteration 5 -- 386array(1) { 387 [0]=> 388 int(0) 389} 390array(1) { 391 [0]=> 392 int(1) 393} 394array(1) { 395 [0]=> 396 int(-1) 397} 398array(1) { 399 [0]=> 400 int(-2147483648) 401} 402array(1) { 403 [0]=> 404 int(-2147483647) 405} 406array(1) { 407 [0]=> 408 int(2147483647) 409} 410array(1) { 411 [0]=> 412 int(2147483640) 413} 414array(1) { 415 [0]=> 416 int(4667) 417} 418array(1) { 419 [0]=> 420 int(4779) 421} 422array(1) { 423 [0]=> 424 int(4095) 425} 426array(1) { 427 [0]=> 428 int(250) 429} 430array(1) { 431 [0]=> 432 int(-2147483648) 433} 434array(1) { 435 [0]=> 436 int(2147483647) 437} 438array(1) { 439 [0]=> 440 int(2147483647) 441} 442array(1) { 443 [0]=> 444 int(83) 445} 446array(1) { 447 [0]=> 448 int(1) 449} 450array(1) { 451 [0]=> 452 int(-2147483648) 453} 454array(1) { 455 [0]=> 456 int(2147483647) 457} 458bool(false) 459 460-- iteration 6 -- 461array(1) { 462 [0]=> 463 int(0) 464} 465array(1) { 466 [0]=> 467 int(1) 468} 469array(1) { 470 [0]=> 471 int(-1) 472} 473array(1) { 474 [0]=> 475 int(-2147483648) 476} 477array(1) { 478 [0]=> 479 int(-2147483647) 480} 481array(1) { 482 [0]=> 483 int(2147483647) 484} 485array(1) { 486 [0]=> 487 int(2147483640) 488} 489array(1) { 490 [0]=> 491 int(4667) 492} 493array(1) { 494 [0]=> 495 int(4779) 496} 497array(1) { 498 [0]=> 499 int(4095) 500} 501array(1) { 502 [0]=> 503 int(250) 504} 505array(1) { 506 [0]=> 507 int(-2147483648) 508} 509array(1) { 510 [0]=> 511 int(2147483647) 512} 513array(1) { 514 [0]=> 515 int(2147483647) 516} 517array(1) { 518 [0]=> 519 int(83) 520} 521array(1) { 522 [0]=> 523 int(1) 524} 525array(1) { 526 [0]=> 527 int(-2147483648) 528} 529array(1) { 530 [0]=> 531 int(2147483647) 532} 533bool(false) 534 535-- iteration 7 -- 536Bad scan conversion character " " 537Bad scan conversion character " " 538Bad scan conversion character " " 539Bad scan conversion character " " 540Bad scan conversion character " " 541Bad scan conversion character " " 542Bad scan conversion character " " 543Bad scan conversion character " " 544Bad scan conversion character " " 545Bad scan conversion character " " 546Bad scan conversion character " " 547Bad scan conversion character " " 548Bad scan conversion character " " 549Bad scan conversion character " " 550Bad scan conversion character " " 551Bad scan conversion character " " 552Bad scan conversion character " " 553Bad scan conversion character " " 554bool(false) 555 556-- iteration 8 -- 557array(1) { 558 [0]=> 559 int(0) 560} 561array(1) { 562 [0]=> 563 int(1) 564} 565array(1) { 566 [0]=> 567 int(-1) 568} 569array(1) { 570 [0]=> 571 int(-2147483648) 572} 573array(1) { 574 [0]=> 575 int(-2147483647) 576} 577array(1) { 578 [0]=> 579 int(2147483647) 580} 581array(1) { 582 [0]=> 583 int(2147483640) 584} 585array(1) { 586 [0]=> 587 int(4667) 588} 589array(1) { 590 [0]=> 591 int(4779) 592} 593array(1) { 594 [0]=> 595 int(4095) 596} 597array(1) { 598 [0]=> 599 int(250) 600} 601array(1) { 602 [0]=> 603 int(-2147483648) 604} 605array(1) { 606 [0]=> 607 int(2147483647) 608} 609array(1) { 610 [0]=> 611 int(2147483647) 612} 613array(1) { 614 [0]=> 615 int(83) 616} 617array(1) { 618 [0]=> 619 int(1) 620} 621array(1) { 622 [0]=> 623 int(-2147483648) 624} 625array(1) { 626 [0]=> 627 int(2147483647) 628} 629bool(false) 630 631-- iteration 9 -- 632array(1) { 633 [0]=> 634 int(0) 635} 636array(1) { 637 [0]=> 638 int(1) 639} 640array(1) { 641 [0]=> 642 int(-1) 643} 644array(1) { 645 [0]=> 646 int(-2147483648) 647} 648array(1) { 649 [0]=> 650 int(-2147483647) 651} 652array(1) { 653 [0]=> 654 int(2147483647) 655} 656array(1) { 657 [0]=> 658 int(2147483640) 659} 660array(1) { 661 [0]=> 662 int(4667) 663} 664array(1) { 665 [0]=> 666 int(4779) 667} 668array(1) { 669 [0]=> 670 int(4095) 671} 672array(1) { 673 [0]=> 674 int(250) 675} 676array(1) { 677 [0]=> 678 int(-2147483648) 679} 680array(1) { 681 [0]=> 682 int(2147483647) 683} 684array(1) { 685 [0]=> 686 int(2147483647) 687} 688array(1) { 689 [0]=> 690 int(83) 691} 692array(1) { 693 [0]=> 694 int(1) 695} 696array(1) { 697 [0]=> 698 int(-2147483648) 699} 700array(1) { 701 [0]=> 702 int(2147483647) 703} 704bool(false) 705 706-- iteration 10 -- 707array(1) { 708 [0]=> 709 int(0) 710} 711array(1) { 712 [0]=> 713 int(1) 714} 715array(1) { 716 [0]=> 717 int(-1) 718} 719array(1) { 720 [0]=> 721 int(-214) 722} 723array(1) { 724 [0]=> 725 int(-214) 726} 727array(1) { 728 [0]=> 729 int(2147) 730} 731array(1) { 732 [0]=> 733 int(2147) 734} 735array(1) { 736 [0]=> 737 int(4667) 738} 739array(1) { 740 [0]=> 741 int(4779) 742} 743array(1) { 744 [0]=> 745 int(4095) 746} 747array(1) { 748 [0]=> 749 int(250) 750} 751array(1) { 752 [0]=> 753 int(-214) 754} 755array(1) { 756 [0]=> 757 int(2147) 758} 759array(1) { 760 [0]=> 761 int(2147) 762} 763array(1) { 764 [0]=> 765 int(83) 766} 767array(1) { 768 [0]=> 769 int(1) 770} 771array(1) { 772 [0]=> 773 int(-214) 774} 775array(1) { 776 [0]=> 777 int(2147) 778} 779bool(false) 780 781-- iteration 11 -- 782array(1) { 783 [0]=> 784 int(0) 785} 786array(1) { 787 [0]=> 788 int(1) 789} 790array(1) { 791 [0]=> 792 int(-1) 793} 794array(1) { 795 [0]=> 796 int(-2147483648) 797} 798array(1) { 799 [0]=> 800 int(-2147483647) 801} 802array(1) { 803 [0]=> 804 int(2147483647) 805} 806array(1) { 807 [0]=> 808 int(2147483640) 809} 810array(1) { 811 [0]=> 812 int(4667) 813} 814array(1) { 815 [0]=> 816 int(4779) 817} 818array(1) { 819 [0]=> 820 int(4095) 821} 822array(1) { 823 [0]=> 824 int(250) 825} 826array(1) { 827 [0]=> 828 int(-2147483648) 829} 830array(1) { 831 [0]=> 832 int(2147483647) 833} 834array(1) { 835 [0]=> 836 int(2147483647) 837} 838array(1) { 839 [0]=> 840 int(83) 841} 842array(1) { 843 [0]=> 844 int(1) 845} 846array(1) { 847 [0]=> 848 int(-2147483648) 849} 850array(1) { 851 [0]=> 852 int(2147483647) 853} 854bool(false) 855 856-- iteration 12 -- 857array(1) { 858 [0]=> 859 string(1) "0" 860} 861array(1) { 862 [0]=> 863 string(1) "1" 864} 865array(1) { 866 [0]=> 867 NULL 868} 869array(1) { 870 [0]=> 871 NULL 872} 873array(1) { 874 [0]=> 875 NULL 876} 877array(1) { 878 [0]=> 879 string(10) "2147483647" 880} 881array(1) { 882 [0]=> 883 string(10) "2147483640" 884} 885array(1) { 886 [0]=> 887 string(4) "4667" 888} 889array(1) { 890 [0]=> 891 string(4) "4779" 892} 893array(1) { 894 [0]=> 895 string(4) "4095" 896} 897array(1) { 898 [0]=> 899 string(3) "250" 900} 901array(1) { 902 [0]=> 903 NULL 904} 905array(1) { 906 [0]=> 907 string(10) "2147483647" 908} 909array(1) { 910 [0]=> 911 string(10) "2147483647" 912} 913array(1) { 914 [0]=> 915 string(2) "83" 916} 917array(1) { 918 [0]=> 919 string(1) "1" 920} 921array(1) { 922 [0]=> 923 NULL 924} 925array(1) { 926 [0]=> 927 string(10) "2147483647" 928} 929bool(false) 930 931-- iteration 13 -- 932array(0) { 933} 934array(0) { 935} 936array(0) { 937} 938array(0) { 939} 940array(0) { 941} 942array(0) { 943} 944array(0) { 945} 946array(0) { 947} 948array(0) { 949} 950array(0) { 951} 952array(0) { 953} 954array(0) { 955} 956array(0) { 957} 958array(0) { 959} 960array(0) { 961} 962array(0) { 963} 964array(0) { 965} 966array(0) { 967} 968bool(false) 969 970*** Done *** 971