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 resource type. 20 Set type of the data to "resource" 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 resource 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 resource type */ 159$type = "resource"; 160 161echo "\n*** Testing gettype() & settype() functions : usage variations ***\n"; 162echo "\n-- Setting type of data to $type --\n"; 163$loop_count = 1; 164foreach ($var_values as $var) { 165 echo "-- Iteration $loop_count --\n"; $loop_count++; 166 167 // get the current data type 168 var_dump( gettype($var) ); 169 170 // convert it to null 171 var_dump( settype($var, $type) ); 172 173 // dump the converted data 174 var_dump( $var ); 175 176 // check the new type after conversion 177 var_dump( gettype($var) ); 178} 179 180echo "Done\n"; 181?> 182--EXPECT-- 1838: Undefined variable: unset_var 1848: Undefined variable: undef_var 185 186*** Testing gettype() & settype() functions : usage variations *** 187 188-- Setting type of data to resource -- 189-- Iteration 1 -- 190string(4) "NULL" 1912: settype(): Cannot convert to resource type 192bool(false) 193NULL 194string(4) "NULL" 195-- Iteration 2 -- 196string(7) "boolean" 1972: settype(): Cannot convert to resource type 198bool(false) 199bool(false) 200string(7) "boolean" 201-- Iteration 3 -- 202string(7) "boolean" 2032: settype(): Cannot convert to resource type 204bool(false) 205bool(true) 206string(7) "boolean" 207-- Iteration 4 -- 208string(7) "boolean" 2092: settype(): Cannot convert to resource type 210bool(false) 211bool(true) 212string(7) "boolean" 213-- Iteration 5 -- 214string(6) "string" 2152: settype(): Cannot convert to resource type 216bool(false) 217string(1) "�" 218string(6) "string" 219-- Iteration 6 -- 220string(6) "string" 2212: settype(): Cannot convert to resource type 222bool(false) 223string(1) "f" 224string(6) "string" 225-- Iteration 7 -- 226string(6) "string" 2272: settype(): Cannot convert to resource type 228bool(false) 229string(2) " 2303" 231string(6) "string" 232-- Iteration 8 -- 233string(6) "string" 2342: settype(): Cannot convert to resource type 235bool(false) 236string(0) "" 237string(6) "string" 238-- Iteration 9 -- 239string(6) "string" 2402: settype(): Cannot convert to resource type 241bool(false) 242string(0) "" 243string(6) "string" 244-- Iteration 10 -- 245string(6) "string" 2462: settype(): Cannot convert to resource type 247bool(false) 248string(1) " " 249string(6) "string" 250-- Iteration 11 -- 251string(6) "string" 2522: settype(): Cannot convert to resource type 253bool(false) 254string(1) " " 255string(6) "string" 256-- Iteration 12 -- 257string(6) "string" 2582: settype(): Cannot convert to resource type 259bool(false) 260string(2) "10" 261string(6) "string" 262-- Iteration 13 -- 263string(6) "string" 2642: settype(): Cannot convert to resource type 265bool(false) 266string(2) "10" 267string(6) "string" 268-- Iteration 14 -- 269string(6) "string" 2702: settype(): Cannot convert to resource type 271bool(false) 272string(8) "10string" 273string(6) "string" 274-- Iteration 15 -- 275string(6) "string" 2762: settype(): Cannot convert to resource type 277bool(false) 278string(8) "10string" 279string(6) "string" 280-- Iteration 16 -- 281string(6) "string" 2822: settype(): Cannot convert to resource type 283bool(false) 284string(1) "1" 285string(6) "string" 286-- Iteration 17 -- 287string(6) "string" 2882: settype(): Cannot convert to resource type 289bool(false) 290string(2) "-1" 291string(6) "string" 292-- Iteration 18 -- 293string(6) "string" 2942: settype(): Cannot convert to resource type 295bool(false) 296string(3) "1e2" 297string(6) "string" 298-- Iteration 19 -- 299string(6) "string" 3002: settype(): Cannot convert to resource type 301bool(false) 302string(2) " 1" 303string(6) "string" 304-- Iteration 20 -- 305string(6) "string" 3062: settype(): Cannot convert to resource type 307bool(false) 308string(22) "2974394749328742328432" 309string(6) "string" 310-- Iteration 21 -- 311string(6) "string" 3122: settype(): Cannot convert to resource type 313bool(false) 314string(5) "-1e-2" 315string(6) "string" 316-- Iteration 22 -- 317string(6) "string" 3182: settype(): Cannot convert to resource type 319bool(false) 320string(1) "1" 321string(6) "string" 322-- Iteration 23 -- 323string(6) "string" 3242: settype(): Cannot convert to resource type 325bool(false) 326string(2) "-1" 327string(6) "string" 328-- Iteration 24 -- 329string(6) "string" 3302: settype(): Cannot convert to resource type 331bool(false) 332string(3) "1e2" 333string(6) "string" 334-- Iteration 25 -- 335string(6) "string" 3362: settype(): Cannot convert to resource type 337bool(false) 338string(2) " 1" 339string(6) "string" 340-- Iteration 26 -- 341string(6) "string" 3422: settype(): Cannot convert to resource type 343bool(false) 344string(22) "2974394749328742328432" 345string(6) "string" 346-- Iteration 27 -- 347string(6) "string" 3482: settype(): Cannot convert to resource type 349bool(false) 350string(5) "-1e-2" 351string(6) "string" 352-- Iteration 28 -- 353string(6) "string" 3542: settype(): Cannot convert to resource type 355bool(false) 356string(4) "0xff" 357string(6) "string" 358-- Iteration 29 -- 359string(6) "string" 3602: settype(): Cannot convert to resource type 361bool(false) 362string(4) "0x55" 363string(6) "string" 364-- Iteration 30 -- 365string(6) "string" 3662: settype(): Cannot convert to resource type 367bool(false) 368string(5) "0XA55" 369string(6) "string" 370-- Iteration 31 -- 371string(6) "string" 3722: settype(): Cannot convert to resource type 373bool(false) 374string(5) "0X123" 375string(6) "string" 376-- Iteration 32 -- 377string(6) "string" 3782: settype(): Cannot convert to resource type 379bool(false) 380string(4) "0123" 381string(6) "string" 382-- Iteration 33 -- 383string(6) "string" 3842: settype(): Cannot convert to resource type 385bool(false) 386string(4) "0123" 387string(6) "string" 388-- Iteration 34 -- 389string(6) "string" 3902: settype(): Cannot convert to resource type 391bool(false) 392string(5) "-0123" 393string(6) "string" 394-- Iteration 35 -- 395string(6) "string" 3962: settype(): Cannot convert to resource type 397bool(false) 398string(5) "+0123" 399string(6) "string" 400-- Iteration 36 -- 401string(6) "string" 4022: settype(): Cannot convert to resource type 403bool(false) 404string(5) "-0123" 405string(6) "string" 406-- Iteration 37 -- 407string(6) "string" 4082: settype(): Cannot convert to resource type 409bool(false) 410string(5) "+0123" 411string(6) "string" 412-- Iteration 38 -- 413string(6) "string" 4142: settype(): Cannot convert to resource type 415bool(false) 416string(8) "-0x80001" 417string(6) "string" 418-- Iteration 39 -- 419string(6) "string" 4202: settype(): Cannot convert to resource type 421bool(false) 422string(8) "+0x80001" 423string(6) "string" 424-- Iteration 40 -- 425string(6) "string" 4262: settype(): Cannot convert to resource type 427bool(false) 428string(10) "-0x80001.5" 429string(6) "string" 430-- Iteration 41 -- 431string(6) "string" 4322: settype(): Cannot convert to resource type 433bool(false) 434string(9) "0x80001.5" 435string(6) "string" 436-- Iteration 42 -- 437string(6) "string" 4382: settype(): Cannot convert to resource type 439bool(false) 440string(12) "@$%#$%^$%^&^" 441string(6) "string" 442-- Iteration 43 -- 443string(5) "array" 4442: settype(): Cannot convert to resource type 445bool(false) 446array(0) { 447} 448string(5) "array" 449-- Iteration 44 -- 450string(5) "array" 4512: settype(): Cannot convert to resource type 452bool(false) 453array(1) { 454 [0]=> 455 NULL 456} 457string(5) "array" 458-- Iteration 45 -- 459string(5) "array" 4602: settype(): Cannot convert to resource type 461bool(false) 462array(4) { 463 [0]=> 464 int(1) 465 [1]=> 466 int(2) 467 [2]=> 468 int(3) 469 [3]=> 470 int(4) 471} 472string(5) "array" 473-- Iteration 46 -- 474string(5) "array" 4752: settype(): Cannot convert to resource type 476bool(false) 477array(4) { 478 [1]=> 479 string(3) "one" 480 [2]=> 481 string(3) "two" 482 [3]=> 483 string(5) "three" 484 ["four"]=> 485 int(4) 486} 487string(5) "array" 488-- Iteration 47 -- 489string(5) "array" 4902: settype(): Cannot convert to resource type 491bool(false) 492array(3) { 493 [0]=> 494 float(1.5) 495 [1]=> 496 float(2.4) 497 [2]=> 498 float(6500000) 499} 500string(5) "array" 501-- Iteration 48 -- 502string(6) "double" 5032: settype(): Cannot convert to resource type 504bool(false) 505float(-2147483648) 506string(6) "double" 507-- Iteration 49 -- 508string(7) "integer" 5092: settype(): Cannot convert to resource type 510bool(false) 511int(2147483647) 512string(7) "integer" 513-- Iteration 50 -- 514string(6) "double" 5152: settype(): Cannot convert to resource type 516bool(false) 517float(2147483649) 518string(6) "double" 519-- Iteration 51 -- 520string(6) "double" 5212: settype(): Cannot convert to resource type 522bool(false) 523float(1232147483649) 524string(6) "double" 525-- Iteration 52 -- 526string(7) "integer" 5272: settype(): Cannot convert to resource type 528bool(false) 529int(85) 530string(7) "integer" 531-- Iteration 53 -- 532string(6) "double" 5332: settype(): Cannot convert to resource type 534bool(false) 535float(1058513956921) 536string(6) "double" 537-- Iteration 54 -- 538string(7) "integer" 5392: settype(): Cannot convert to resource type 540bool(false) 541int(-21903) 542string(7) "integer" 543-- Iteration 55 -- 544string(7) "integer" 5452: settype(): Cannot convert to resource type 546bool(false) 547int(365) 548string(7) "integer" 549-- Iteration 56 -- 550string(7) "integer" 5512: settype(): Cannot convert to resource type 552bool(false) 553int(-365) 554string(7) "integer" 555-- Iteration 57 -- 556string(6) "double" 5572: settype(): Cannot convert to resource type 558bool(false) 559float(80561044571754) 560string(6) "double" 561-- Iteration 58 -- 562string(6) "double" 5632: settype(): Cannot convert to resource type 564bool(false) 565float(100000) 566string(6) "double" 567-- Iteration 59 -- 568string(6) "double" 5692: settype(): Cannot convert to resource type 570bool(false) 571float(-100000) 572string(6) "double" 573-- Iteration 60 -- 574string(6) "double" 5752: settype(): Cannot convert to resource type 576bool(false) 577float(100000) 578string(6) "double" 579-- Iteration 61 -- 580string(6) "double" 5812: settype(): Cannot convert to resource type 582bool(false) 583float(-100000) 584string(6) "double" 585-- Iteration 62 -- 586string(6) "double" 5872: settype(): Cannot convert to resource type 588bool(false) 589float(-1.5) 590string(6) "double" 591-- Iteration 63 -- 592string(6) "double" 5932: settype(): Cannot convert to resource type 594bool(false) 595float(0.5) 596string(6) "double" 597-- Iteration 64 -- 598string(6) "double" 5992: settype(): Cannot convert to resource type 600bool(false) 601float(-0.5) 602string(6) "double" 603-- Iteration 65 -- 604string(6) "double" 6052: settype(): Cannot convert to resource type 606bool(false) 607float(500000) 608string(6) "double" 609-- Iteration 66 -- 610string(6) "double" 6112: settype(): Cannot convert to resource type 612bool(false) 613float(-500000) 614string(6) "double" 615-- Iteration 67 -- 616string(6) "double" 6172: settype(): Cannot convert to resource type 618bool(false) 619float(-5.0E-7) 620string(6) "double" 621-- Iteration 68 -- 622string(6) "double" 6232: settype(): Cannot convert to resource type 624bool(false) 625float(500000) 626string(6) "double" 627-- Iteration 69 -- 628string(6) "double" 6292: settype(): Cannot convert to resource type 630bool(false) 631float(-500000) 632string(6) "double" 633-- Iteration 70 -- 634string(6) "double" 6352: settype(): Cannot convert to resource type 636bool(false) 637float(512000) 638string(6) "double" 639-- Iteration 71 -- 640string(6) "double" 6412: settype(): Cannot convert to resource type 642bool(false) 643float(-512000) 644string(6) "double" 645-- Iteration 72 -- 646string(6) "double" 6472: settype(): Cannot convert to resource type 648bool(false) 649float(5.12E-7) 650string(6) "double" 651-- Iteration 73 -- 652string(6) "double" 6532: settype(): Cannot convert to resource type 654bool(false) 655float(5.12E-7) 656string(6) "double" 657-- Iteration 74 -- 658string(6) "double" 6592: settype(): Cannot convert to resource type 660bool(false) 661float(512000) 662string(6) "double" 663-- Iteration 75 -- 664string(6) "double" 6652: settype(): Cannot convert to resource type 666bool(false) 667float(-512000) 668string(6) "double" 669-- Iteration 76 -- 670string(6) "object" 6712: settype(): Cannot convert to resource type 672bool(false) 673object(point)#1 (2) { 674 ["x"]=> 675 NULL 676 ["y"]=> 677 NULL 678} 679string(6) "object" 680-- Iteration 77 -- 681string(6) "object" 6822: settype(): Cannot convert to resource type 683bool(false) 684object(point)#2 (2) { 685 ["x"]=> 686 float(2.5) 687 ["y"]=> 688 float(40.5) 689} 690string(6) "object" 691-- Iteration 78 -- 692string(6) "object" 6932: settype(): Cannot convert to resource type 694bool(false) 695object(point)#3 (2) { 696 ["x"]=> 697 int(0) 698 ["y"]=> 699 int(0) 700} 701string(6) "object" 702-- Iteration 79 -- 703string(4) "NULL" 7042: settype(): Cannot convert to resource type 705bool(false) 706NULL 707string(4) "NULL" 708-- Iteration 80 -- 709string(4) "NULL" 7102: settype(): Cannot convert to resource type 711bool(false) 712NULL 713string(4) "NULL" 714Done 715