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