1--TEST-- 2Test gettype() & settype() functions : usage variations 3--SKIPIF-- 4<?php 5if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only"); 6 7if (PHP_OS_FAMILY === 'Darwin') { 8 die('skip Do not run on MacOS'); 9} 10?> 11--INI-- 12precision=14 13--FILE-- 14<?php 15/* Prototype: string gettype ( mixed $var ); 16 Description: Returns the type of the PHP variable var 17 18 Prototype: bool settype ( mixed &$var, string $type ); 19 Description: Set the type of variable var to type 20*/ 21 22/* Test usage variation of gettype() and settype() functions: 23 settype() to int/integer type. 24 Set type of the data to "int"/"integer" and verify using gettype 25 Following are performed in the listed sequence: 26 get the current type of the variable 27 set the type of the variable to interger/int type 28 dump the variable to see its new data 29 get the new type of the variable 30*/ 31 32/* function to handle catchable errors */ 33function foo($errno, $errstr, $errfile, $errline) { 34// var_dump($errstr); 35 // print error no and error string 36 echo "$errno: $errstr\n"; 37} 38//set the error handler, this is required as 39// settype() would fail with catachable fatal error 40set_error_handler("foo"); 41 42$var1 = "another string"; 43$var2 = array(2,3,4); 44 45// a variable which is unset 46$unset_var = 10.5; 47unset( $unset_var ); 48 49class point 50{ 51 var $x; 52 var $y; 53 54 function __construct($x, $y) { 55 $this->x = $x; 56 $this->y = $y; 57 } 58 59 function __toString() { 60 return "ObjectPoint"; 61 } 62} 63 64$var_values = array ( 65 /* nulls */ 66 null, 67 68 /* boolean */ 69 FALSE, 70 TRUE, 71 true, 72 73 /* strings */ 74 "\xFF", 75 "\x66", 76 "\0123", 77 "", 78 '', 79 " ", 80 ' ', 81 /* numerics in the form of string */ 82 '10', 83 "10", 84 "10string", 85 '10string', 86 "1", 87 "-1", 88 "1e2", 89 " 1", 90 "2974394749328742328432", 91 "-1e-2", 92 '1', 93 '-1', 94 '1e2', 95 ' 1', 96 '2974394749328742328432', 97 '-1e-2', 98 "0xff", 99 '0x55', 100 '0XA55', 101 '0X123', 102 "0123", 103 '0123', 104 "-0123", 105 "+0123", 106 '-0123', 107 '+0123', 108 "-0x80001", // invalid numerics as its prefix with sign or have decimal points 109 "+0x80001", 110 "-0x80001.5", 111 "0x80001.5", 112 "@$%#$%^$%^&^", 113 114 /* arrays */ 115 array(), 116 array(NULL), 117 array(1,2,3,4), 118 array(1 => "one", 2 => "two", "3" => "three", "four" => 4), 119 array(1.5, 2.4, 6.5e6), 120 121 /* integers */ 122 -2147483648, // max -ne int value 123 2147483647, 124 2147483649, 125 1232147483649, 126 0x55, 127 0xF674593039, // a hex value > than max int 128 -0X558F, 129 0555, 130 -0555, 131 02224242434343152, // an octal value > than max int 132 133 /* floats */ 134 1e5, 135 -1e5, 136 1E5, 137 -1E5, 138 -1.5, 139 .5, 140 -.5, 141 .5e6, 142 -.5e6, 143 -.5e-6, 144 .5e+6, 145 -.5e+6, 146 .512E6, 147 -.512E6, 148 .512E-6, 149 +.512E-6, 150 .512E+6, 151 -.512E+6, 152 153 new point(NULL, NULL), 154 new point(2.5, 40.5), 155 new point(0, 0), 156 157 /* undefined/unset vars */ 158 $unset_var, 159 $undef_var 160); 161 162// test conversion to these types 163$types = array( 164 "integer", 165 "int" 166); 167 168echo "\n*** Testing settype() & gettype() : usage variations ***\n"; 169foreach ($types as $type) { 170 echo "\n-- Setting type of data to $type --\n"; 171 $inner_loop_count = 1; 172 foreach ($var_values as $var) { 173 echo "-- Iteration $inner_loop_count --\n"; $inner_loop_count++; 174 175 // get the current data type 176 var_dump( gettype($var) ); 177 178 // convert it to new type 179 var_dump( settype($var, $type) ); 180 181 // dump the converted $var 182 var_dump( $var ); 183 184 // get the new type of the $var 185 var_dump( gettype($var) ); 186 } 187} 188 189echo "Done\n"; 190?> 191--EXPECT-- 1928: Undefined variable: unset_var 1938: Undefined variable: undef_var 194 195*** Testing settype() & gettype() : usage variations *** 196 197-- Setting type of data to integer -- 198-- Iteration 1 -- 199string(4) "NULL" 200bool(true) 201int(0) 202string(7) "integer" 203-- Iteration 2 -- 204string(7) "boolean" 205bool(true) 206int(0) 207string(7) "integer" 208-- Iteration 3 -- 209string(7) "boolean" 210bool(true) 211int(1) 212string(7) "integer" 213-- Iteration 4 -- 214string(7) "boolean" 215bool(true) 216int(1) 217string(7) "integer" 218-- Iteration 5 -- 219string(6) "string" 220bool(true) 221int(0) 222string(7) "integer" 223-- Iteration 6 -- 224string(6) "string" 225bool(true) 226int(0) 227string(7) "integer" 228-- Iteration 7 -- 229string(6) "string" 230bool(true) 231int(3) 232string(7) "integer" 233-- Iteration 8 -- 234string(6) "string" 235bool(true) 236int(0) 237string(7) "integer" 238-- Iteration 9 -- 239string(6) "string" 240bool(true) 241int(0) 242string(7) "integer" 243-- Iteration 10 -- 244string(6) "string" 245bool(true) 246int(0) 247string(7) "integer" 248-- Iteration 11 -- 249string(6) "string" 250bool(true) 251int(0) 252string(7) "integer" 253-- Iteration 12 -- 254string(6) "string" 255bool(true) 256int(10) 257string(7) "integer" 258-- Iteration 13 -- 259string(6) "string" 260bool(true) 261int(10) 262string(7) "integer" 263-- Iteration 14 -- 264string(6) "string" 265bool(true) 266int(10) 267string(7) "integer" 268-- Iteration 15 -- 269string(6) "string" 270bool(true) 271int(10) 272string(7) "integer" 273-- Iteration 16 -- 274string(6) "string" 275bool(true) 276int(1) 277string(7) "integer" 278-- Iteration 17 -- 279string(6) "string" 280bool(true) 281int(-1) 282string(7) "integer" 283-- Iteration 18 -- 284string(6) "string" 285bool(true) 286int(100) 287string(7) "integer" 288-- Iteration 19 -- 289string(6) "string" 290bool(true) 291int(1) 292string(7) "integer" 293-- Iteration 20 -- 294string(6) "string" 295bool(true) 296int(2147483647) 297string(7) "integer" 298-- Iteration 21 -- 299string(6) "string" 300bool(true) 301int(0) 302string(7) "integer" 303-- Iteration 22 -- 304string(6) "string" 305bool(true) 306int(1) 307string(7) "integer" 308-- Iteration 23 -- 309string(6) "string" 310bool(true) 311int(-1) 312string(7) "integer" 313-- Iteration 24 -- 314string(6) "string" 315bool(true) 316int(100) 317string(7) "integer" 318-- Iteration 25 -- 319string(6) "string" 320bool(true) 321int(1) 322string(7) "integer" 323-- Iteration 26 -- 324string(6) "string" 325bool(true) 326int(2147483647) 327string(7) "integer" 328-- Iteration 27 -- 329string(6) "string" 330bool(true) 331int(0) 332string(7) "integer" 333-- Iteration 28 -- 334string(6) "string" 335bool(true) 336int(0) 337string(7) "integer" 338-- Iteration 29 -- 339string(6) "string" 340bool(true) 341int(0) 342string(7) "integer" 343-- Iteration 30 -- 344string(6) "string" 345bool(true) 346int(0) 347string(7) "integer" 348-- Iteration 31 -- 349string(6) "string" 350bool(true) 351int(0) 352string(7) "integer" 353-- Iteration 32 -- 354string(6) "string" 355bool(true) 356int(123) 357string(7) "integer" 358-- Iteration 33 -- 359string(6) "string" 360bool(true) 361int(123) 362string(7) "integer" 363-- Iteration 34 -- 364string(6) "string" 365bool(true) 366int(-123) 367string(7) "integer" 368-- Iteration 35 -- 369string(6) "string" 370bool(true) 371int(123) 372string(7) "integer" 373-- Iteration 36 -- 374string(6) "string" 375bool(true) 376int(-123) 377string(7) "integer" 378-- Iteration 37 -- 379string(6) "string" 380bool(true) 381int(123) 382string(7) "integer" 383-- Iteration 38 -- 384string(6) "string" 385bool(true) 386int(0) 387string(7) "integer" 388-- Iteration 39 -- 389string(6) "string" 390bool(true) 391int(0) 392string(7) "integer" 393-- Iteration 40 -- 394string(6) "string" 395bool(true) 396int(0) 397string(7) "integer" 398-- Iteration 41 -- 399string(6) "string" 400bool(true) 401int(0) 402string(7) "integer" 403-- Iteration 42 -- 404string(6) "string" 405bool(true) 406int(0) 407string(7) "integer" 408-- Iteration 43 -- 409string(5) "array" 410bool(true) 411int(0) 412string(7) "integer" 413-- Iteration 44 -- 414string(5) "array" 415bool(true) 416int(1) 417string(7) "integer" 418-- Iteration 45 -- 419string(5) "array" 420bool(true) 421int(1) 422string(7) "integer" 423-- Iteration 46 -- 424string(5) "array" 425bool(true) 426int(1) 427string(7) "integer" 428-- Iteration 47 -- 429string(5) "array" 430bool(true) 431int(1) 432string(7) "integer" 433-- Iteration 48 -- 434string(6) "double" 435bool(true) 436int(-2147483648) 437string(7) "integer" 438-- Iteration 49 -- 439string(7) "integer" 440bool(true) 441int(2147483647) 442string(7) "integer" 443-- Iteration 50 -- 444string(6) "double" 445bool(true) 446int(-2147483647) 447string(7) "integer" 448-- Iteration 51 -- 449string(6) "double" 450bool(true) 451int(-508130303) 452string(7) "integer" 453-- Iteration 52 -- 454string(7) "integer" 455bool(true) 456int(85) 457string(7) "integer" 458-- Iteration 53 -- 459string(6) "double" 460bool(true) 461int(1952002105) 462string(7) "integer" 463-- Iteration 54 -- 464string(7) "integer" 465bool(true) 466int(-21903) 467string(7) "integer" 468-- Iteration 55 -- 469string(7) "integer" 470bool(true) 471int(365) 472string(7) "integer" 473-- Iteration 56 -- 474string(7) "integer" 475bool(true) 476int(-365) 477string(7) "integer" 478-- Iteration 57 -- 479string(6) "double" 480bool(true) 481int(343000682) 482string(7) "integer" 483-- Iteration 58 -- 484string(6) "double" 485bool(true) 486int(100000) 487string(7) "integer" 488-- Iteration 59 -- 489string(6) "double" 490bool(true) 491int(-100000) 492string(7) "integer" 493-- Iteration 60 -- 494string(6) "double" 495bool(true) 496int(100000) 497string(7) "integer" 498-- Iteration 61 -- 499string(6) "double" 500bool(true) 501int(-100000) 502string(7) "integer" 503-- Iteration 62 -- 504string(6) "double" 505bool(true) 506int(-1) 507string(7) "integer" 508-- Iteration 63 -- 509string(6) "double" 510bool(true) 511int(0) 512string(7) "integer" 513-- Iteration 64 -- 514string(6) "double" 515bool(true) 516int(0) 517string(7) "integer" 518-- Iteration 65 -- 519string(6) "double" 520bool(true) 521int(500000) 522string(7) "integer" 523-- Iteration 66 -- 524string(6) "double" 525bool(true) 526int(-500000) 527string(7) "integer" 528-- Iteration 67 -- 529string(6) "double" 530bool(true) 531int(0) 532string(7) "integer" 533-- Iteration 68 -- 534string(6) "double" 535bool(true) 536int(500000) 537string(7) "integer" 538-- Iteration 69 -- 539string(6) "double" 540bool(true) 541int(-500000) 542string(7) "integer" 543-- Iteration 70 -- 544string(6) "double" 545bool(true) 546int(512000) 547string(7) "integer" 548-- Iteration 71 -- 549string(6) "double" 550bool(true) 551int(-512000) 552string(7) "integer" 553-- Iteration 72 -- 554string(6) "double" 555bool(true) 556int(0) 557string(7) "integer" 558-- Iteration 73 -- 559string(6) "double" 560bool(true) 561int(0) 562string(7) "integer" 563-- Iteration 74 -- 564string(6) "double" 565bool(true) 566int(512000) 567string(7) "integer" 568-- Iteration 75 -- 569string(6) "double" 570bool(true) 571int(-512000) 572string(7) "integer" 573-- Iteration 76 -- 574string(6) "object" 5758: Object of class point could not be converted to int 576bool(true) 577int(1) 578string(7) "integer" 579-- Iteration 77 -- 580string(6) "object" 5818: Object of class point could not be converted to int 582bool(true) 583int(1) 584string(7) "integer" 585-- Iteration 78 -- 586string(6) "object" 5878: Object of class point could not be converted to int 588bool(true) 589int(1) 590string(7) "integer" 591-- Iteration 79 -- 592string(4) "NULL" 593bool(true) 594int(0) 595string(7) "integer" 596-- Iteration 80 -- 597string(4) "NULL" 598bool(true) 599int(0) 600string(7) "integer" 601 602-- Setting type of data to int -- 603-- Iteration 1 -- 604string(4) "NULL" 605bool(true) 606int(0) 607string(7) "integer" 608-- Iteration 2 -- 609string(7) "boolean" 610bool(true) 611int(0) 612string(7) "integer" 613-- Iteration 3 -- 614string(7) "boolean" 615bool(true) 616int(1) 617string(7) "integer" 618-- Iteration 4 -- 619string(7) "boolean" 620bool(true) 621int(1) 622string(7) "integer" 623-- Iteration 5 -- 624string(6) "string" 625bool(true) 626int(0) 627string(7) "integer" 628-- Iteration 6 -- 629string(6) "string" 630bool(true) 631int(0) 632string(7) "integer" 633-- Iteration 7 -- 634string(6) "string" 635bool(true) 636int(3) 637string(7) "integer" 638-- Iteration 8 -- 639string(6) "string" 640bool(true) 641int(0) 642string(7) "integer" 643-- Iteration 9 -- 644string(6) "string" 645bool(true) 646int(0) 647string(7) "integer" 648-- Iteration 10 -- 649string(6) "string" 650bool(true) 651int(0) 652string(7) "integer" 653-- Iteration 11 -- 654string(6) "string" 655bool(true) 656int(0) 657string(7) "integer" 658-- Iteration 12 -- 659string(6) "string" 660bool(true) 661int(10) 662string(7) "integer" 663-- Iteration 13 -- 664string(6) "string" 665bool(true) 666int(10) 667string(7) "integer" 668-- Iteration 14 -- 669string(6) "string" 670bool(true) 671int(10) 672string(7) "integer" 673-- Iteration 15 -- 674string(6) "string" 675bool(true) 676int(10) 677string(7) "integer" 678-- Iteration 16 -- 679string(6) "string" 680bool(true) 681int(1) 682string(7) "integer" 683-- Iteration 17 -- 684string(6) "string" 685bool(true) 686int(-1) 687string(7) "integer" 688-- Iteration 18 -- 689string(6) "string" 690bool(true) 691int(100) 692string(7) "integer" 693-- Iteration 19 -- 694string(6) "string" 695bool(true) 696int(1) 697string(7) "integer" 698-- Iteration 20 -- 699string(6) "string" 700bool(true) 701int(2147483647) 702string(7) "integer" 703-- Iteration 21 -- 704string(6) "string" 705bool(true) 706int(0) 707string(7) "integer" 708-- Iteration 22 -- 709string(6) "string" 710bool(true) 711int(1) 712string(7) "integer" 713-- Iteration 23 -- 714string(6) "string" 715bool(true) 716int(-1) 717string(7) "integer" 718-- Iteration 24 -- 719string(6) "string" 720bool(true) 721int(100) 722string(7) "integer" 723-- Iteration 25 -- 724string(6) "string" 725bool(true) 726int(1) 727string(7) "integer" 728-- Iteration 26 -- 729string(6) "string" 730bool(true) 731int(2147483647) 732string(7) "integer" 733-- Iteration 27 -- 734string(6) "string" 735bool(true) 736int(0) 737string(7) "integer" 738-- Iteration 28 -- 739string(6) "string" 740bool(true) 741int(0) 742string(7) "integer" 743-- Iteration 29 -- 744string(6) "string" 745bool(true) 746int(0) 747string(7) "integer" 748-- Iteration 30 -- 749string(6) "string" 750bool(true) 751int(0) 752string(7) "integer" 753-- Iteration 31 -- 754string(6) "string" 755bool(true) 756int(0) 757string(7) "integer" 758-- Iteration 32 -- 759string(6) "string" 760bool(true) 761int(123) 762string(7) "integer" 763-- Iteration 33 -- 764string(6) "string" 765bool(true) 766int(123) 767string(7) "integer" 768-- Iteration 34 -- 769string(6) "string" 770bool(true) 771int(-123) 772string(7) "integer" 773-- Iteration 35 -- 774string(6) "string" 775bool(true) 776int(123) 777string(7) "integer" 778-- Iteration 36 -- 779string(6) "string" 780bool(true) 781int(-123) 782string(7) "integer" 783-- Iteration 37 -- 784string(6) "string" 785bool(true) 786int(123) 787string(7) "integer" 788-- Iteration 38 -- 789string(6) "string" 790bool(true) 791int(0) 792string(7) "integer" 793-- Iteration 39 -- 794string(6) "string" 795bool(true) 796int(0) 797string(7) "integer" 798-- Iteration 40 -- 799string(6) "string" 800bool(true) 801int(0) 802string(7) "integer" 803-- Iteration 41 -- 804string(6) "string" 805bool(true) 806int(0) 807string(7) "integer" 808-- Iteration 42 -- 809string(6) "string" 810bool(true) 811int(0) 812string(7) "integer" 813-- Iteration 43 -- 814string(5) "array" 815bool(true) 816int(0) 817string(7) "integer" 818-- Iteration 44 -- 819string(5) "array" 820bool(true) 821int(1) 822string(7) "integer" 823-- Iteration 45 -- 824string(5) "array" 825bool(true) 826int(1) 827string(7) "integer" 828-- Iteration 46 -- 829string(5) "array" 830bool(true) 831int(1) 832string(7) "integer" 833-- Iteration 47 -- 834string(5) "array" 835bool(true) 836int(1) 837string(7) "integer" 838-- Iteration 48 -- 839string(6) "double" 840bool(true) 841int(-2147483648) 842string(7) "integer" 843-- Iteration 49 -- 844string(7) "integer" 845bool(true) 846int(2147483647) 847string(7) "integer" 848-- Iteration 50 -- 849string(6) "double" 850bool(true) 851int(-2147483647) 852string(7) "integer" 853-- Iteration 51 -- 854string(6) "double" 855bool(true) 856int(-508130303) 857string(7) "integer" 858-- Iteration 52 -- 859string(7) "integer" 860bool(true) 861int(85) 862string(7) "integer" 863-- Iteration 53 -- 864string(6) "double" 865bool(true) 866int(1952002105) 867string(7) "integer" 868-- Iteration 54 -- 869string(7) "integer" 870bool(true) 871int(-21903) 872string(7) "integer" 873-- Iteration 55 -- 874string(7) "integer" 875bool(true) 876int(365) 877string(7) "integer" 878-- Iteration 56 -- 879string(7) "integer" 880bool(true) 881int(-365) 882string(7) "integer" 883-- Iteration 57 -- 884string(6) "double" 885bool(true) 886int(343000682) 887string(7) "integer" 888-- Iteration 58 -- 889string(6) "double" 890bool(true) 891int(100000) 892string(7) "integer" 893-- Iteration 59 -- 894string(6) "double" 895bool(true) 896int(-100000) 897string(7) "integer" 898-- Iteration 60 -- 899string(6) "double" 900bool(true) 901int(100000) 902string(7) "integer" 903-- Iteration 61 -- 904string(6) "double" 905bool(true) 906int(-100000) 907string(7) "integer" 908-- Iteration 62 -- 909string(6) "double" 910bool(true) 911int(-1) 912string(7) "integer" 913-- Iteration 63 -- 914string(6) "double" 915bool(true) 916int(0) 917string(7) "integer" 918-- Iteration 64 -- 919string(6) "double" 920bool(true) 921int(0) 922string(7) "integer" 923-- Iteration 65 -- 924string(6) "double" 925bool(true) 926int(500000) 927string(7) "integer" 928-- Iteration 66 -- 929string(6) "double" 930bool(true) 931int(-500000) 932string(7) "integer" 933-- Iteration 67 -- 934string(6) "double" 935bool(true) 936int(0) 937string(7) "integer" 938-- Iteration 68 -- 939string(6) "double" 940bool(true) 941int(500000) 942string(7) "integer" 943-- Iteration 69 -- 944string(6) "double" 945bool(true) 946int(-500000) 947string(7) "integer" 948-- Iteration 70 -- 949string(6) "double" 950bool(true) 951int(512000) 952string(7) "integer" 953-- Iteration 71 -- 954string(6) "double" 955bool(true) 956int(-512000) 957string(7) "integer" 958-- Iteration 72 -- 959string(6) "double" 960bool(true) 961int(0) 962string(7) "integer" 963-- Iteration 73 -- 964string(6) "double" 965bool(true) 966int(0) 967string(7) "integer" 968-- Iteration 74 -- 969string(6) "double" 970bool(true) 971int(512000) 972string(7) "integer" 973-- Iteration 75 -- 974string(6) "double" 975bool(true) 976int(-512000) 977string(7) "integer" 978-- Iteration 76 -- 979string(6) "object" 9808: Object of class point could not be converted to int 981bool(true) 982int(1) 983string(7) "integer" 984-- Iteration 77 -- 985string(6) "object" 9868: Object of class point could not be converted to int 987bool(true) 988int(1) 989string(7) "integer" 990-- Iteration 78 -- 991string(6) "object" 9928: Object of class point could not be converted to int 993bool(true) 994int(1) 995string(7) "integer" 996-- Iteration 79 -- 997string(4) "NULL" 998bool(true) 999int(0) 1000string(7) "integer" 1001-- Iteration 80 -- 1002string(4) "NULL" 1003bool(true) 1004int(0) 1005string(7) "integer" 1006Done 1007