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