1--TEST-- 2Test gettype() & settype() functions : basic functionalities 3--INI-- 4precision=14 5--FILE-- 6<?php 7/* Prototype: string gettype ( mixed $var ); 8 Description: Returns the type of the PHP variable var 9 10 Prototype: bool settype ( mixed &$var, string $type ); 11 Description: Set the type of variable var to type 12*/ 13 14/* Test the basic functionalities of settype() & gettype() functions. 15 Use the gettype() to get the type of regular data and use settype() 16 to change its type to other types */ 17 18/* function to handle catchable errors */ 19function foo($errno, $errstr, $errfile, $errline) { 20// var_dump($errstr); 21 // print error no and error string 22 echo "$errno: $errstr\n"; 23} 24//set the error handler, this is required as 25// settype() would fail with catachable fatal error 26set_error_handler("foo"); 27 28echo "**** Testing gettype() and settype() functions ****\n"; 29 30$fp = fopen(__FILE__, "r"); 31$dfp = opendir( dirname(__FILE__) ); 32 33$var1 = "another string"; 34$var2 = array(2,3,4); 35 36class point 37{ 38 var $x; 39 var $y; 40 41 function __construct($x, $y) { 42 $this->x = $x; 43 $this->y = $y; 44 } 45 46 function __toString() { 47 return "Object"; 48 } 49} 50 51$unset_var = 10; 52unset($unset_var); 53 54$values = array( 55 array(1,2,3), 56 $var1, 57 $var2, 58 1, 59 -20, 60 2.54, 61 -2.54, 62 NULL, 63 false, 64 "some string", 65 'string', 66 $fp, 67 $dfp, 68 new point(10,20) 69); 70 71$types = array( 72 "null", 73 "integer", 74 "int", 75 "float", 76 "double", 77 "boolean", 78 "bool", 79 "resource", 80 "array", 81 "object", 82 "string" 83); 84 85echo "\n*** Testing gettype(): basic operations ***\n"; 86foreach ($values as $value) { 87 var_dump( gettype($value) ); 88} 89 90echo "\n*** Testing settype(): basic operations ***\n"; 91foreach ($types as $type) { 92 echo "\n-- Setting type of data to $type --\n"; 93 $loop_count = 1; 94 foreach ($values as $var) { 95 echo "-- Iteration $loop_count --\n"; $loop_count ++; 96 // set to new type 97 var_dump( settype($var, $type) ); 98 99 // dump the var 100 var_dump( $var ); 101 102 // check the new type 103 var_dump( gettype($var) ); 104 } 105} 106 107echo "Done\n"; 108?> 109--EXPECTF-- 110**** Testing gettype() and settype() functions **** 111 112*** Testing gettype(): basic operations *** 113string(5) "array" 114string(6) "string" 115string(5) "array" 116string(7) "integer" 117string(7) "integer" 118string(6) "double" 119string(6) "double" 120string(4) "NULL" 121string(7) "boolean" 122string(6) "string" 123string(6) "string" 124string(8) "resource" 125string(8) "resource" 126string(6) "object" 127 128*** Testing settype(): basic operations *** 129 130-- Setting type of data to null -- 131-- Iteration 1 -- 132bool(true) 133NULL 134string(4) "NULL" 135-- Iteration 2 -- 136bool(true) 137NULL 138string(4) "NULL" 139-- Iteration 3 -- 140bool(true) 141NULL 142string(4) "NULL" 143-- Iteration 4 -- 144bool(true) 145NULL 146string(4) "NULL" 147-- Iteration 5 -- 148bool(true) 149NULL 150string(4) "NULL" 151-- Iteration 6 -- 152bool(true) 153NULL 154string(4) "NULL" 155-- Iteration 7 -- 156bool(true) 157NULL 158string(4) "NULL" 159-- Iteration 8 -- 160bool(true) 161NULL 162string(4) "NULL" 163-- Iteration 9 -- 164bool(true) 165NULL 166string(4) "NULL" 167-- Iteration 10 -- 168bool(true) 169NULL 170string(4) "NULL" 171-- Iteration 11 -- 172bool(true) 173NULL 174string(4) "NULL" 175-- Iteration 12 -- 176bool(true) 177NULL 178string(4) "NULL" 179-- Iteration 13 -- 180bool(true) 181NULL 182string(4) "NULL" 183-- Iteration 14 -- 184bool(true) 185NULL 186string(4) "NULL" 187 188-- Setting type of data to integer -- 189-- Iteration 1 -- 190bool(true) 191int(1) 192string(7) "integer" 193-- Iteration 2 -- 194bool(true) 195int(0) 196string(7) "integer" 197-- Iteration 3 -- 198bool(true) 199int(1) 200string(7) "integer" 201-- Iteration 4 -- 202bool(true) 203int(1) 204string(7) "integer" 205-- Iteration 5 -- 206bool(true) 207int(-20) 208string(7) "integer" 209-- Iteration 6 -- 210bool(true) 211int(2) 212string(7) "integer" 213-- Iteration 7 -- 214bool(true) 215int(-2) 216string(7) "integer" 217-- Iteration 8 -- 218bool(true) 219int(0) 220string(7) "integer" 221-- Iteration 9 -- 222bool(true) 223int(0) 224string(7) "integer" 225-- Iteration 10 -- 226bool(true) 227int(0) 228string(7) "integer" 229-- Iteration 11 -- 230bool(true) 231int(0) 232string(7) "integer" 233-- Iteration 12 -- 234bool(true) 235int(%d) 236string(7) "integer" 237-- Iteration 13 -- 238bool(true) 239int(%d) 240string(7) "integer" 241-- Iteration 14 -- 2428: Object of class point could not be converted to int 243bool(true) 244int(1) 245string(7) "integer" 246 247-- Setting type of data to int -- 248-- Iteration 1 -- 249bool(true) 250int(1) 251string(7) "integer" 252-- Iteration 2 -- 253bool(true) 254int(0) 255string(7) "integer" 256-- Iteration 3 -- 257bool(true) 258int(1) 259string(7) "integer" 260-- Iteration 4 -- 261bool(true) 262int(1) 263string(7) "integer" 264-- Iteration 5 -- 265bool(true) 266int(-20) 267string(7) "integer" 268-- Iteration 6 -- 269bool(true) 270int(2) 271string(7) "integer" 272-- Iteration 7 -- 273bool(true) 274int(-2) 275string(7) "integer" 276-- Iteration 8 -- 277bool(true) 278int(0) 279string(7) "integer" 280-- Iteration 9 -- 281bool(true) 282int(0) 283string(7) "integer" 284-- Iteration 10 -- 285bool(true) 286int(0) 287string(7) "integer" 288-- Iteration 11 -- 289bool(true) 290int(0) 291string(7) "integer" 292-- Iteration 12 -- 293bool(true) 294int(%d) 295string(7) "integer" 296-- Iteration 13 -- 297bool(true) 298int(%d) 299string(7) "integer" 300-- Iteration 14 -- 3018: Object of class point could not be converted to int 302bool(true) 303int(1) 304string(7) "integer" 305 306-- Setting type of data to float -- 307-- Iteration 1 -- 308bool(true) 309float(1) 310string(6) "double" 311-- Iteration 2 -- 312bool(true) 313float(0) 314string(6) "double" 315-- Iteration 3 -- 316bool(true) 317float(1) 318string(6) "double" 319-- Iteration 4 -- 320bool(true) 321float(1) 322string(6) "double" 323-- Iteration 5 -- 324bool(true) 325float(-20) 326string(6) "double" 327-- Iteration 6 -- 328bool(true) 329float(2.54) 330string(6) "double" 331-- Iteration 7 -- 332bool(true) 333float(-2.54) 334string(6) "double" 335-- Iteration 8 -- 336bool(true) 337float(0) 338string(6) "double" 339-- Iteration 9 -- 340bool(true) 341float(0) 342string(6) "double" 343-- Iteration 10 -- 344bool(true) 345float(0) 346string(6) "double" 347-- Iteration 11 -- 348bool(true) 349float(0) 350string(6) "double" 351-- Iteration 12 -- 352bool(true) 353float(%d) 354string(6) "double" 355-- Iteration 13 -- 356bool(true) 357float(%d) 358string(6) "double" 359-- Iteration 14 -- 3608: Object of class point could not be converted to float 361bool(true) 362float(1) 363string(6) "double" 364 365-- Setting type of data to double -- 366-- Iteration 1 -- 367bool(true) 368float(1) 369string(6) "double" 370-- Iteration 2 -- 371bool(true) 372float(0) 373string(6) "double" 374-- Iteration 3 -- 375bool(true) 376float(1) 377string(6) "double" 378-- Iteration 4 -- 379bool(true) 380float(1) 381string(6) "double" 382-- Iteration 5 -- 383bool(true) 384float(-20) 385string(6) "double" 386-- Iteration 6 -- 387bool(true) 388float(2.54) 389string(6) "double" 390-- Iteration 7 -- 391bool(true) 392float(-2.54) 393string(6) "double" 394-- Iteration 8 -- 395bool(true) 396float(0) 397string(6) "double" 398-- Iteration 9 -- 399bool(true) 400float(0) 401string(6) "double" 402-- Iteration 10 -- 403bool(true) 404float(0) 405string(6) "double" 406-- Iteration 11 -- 407bool(true) 408float(0) 409string(6) "double" 410-- Iteration 12 -- 411bool(true) 412float(%d) 413string(6) "double" 414-- Iteration 13 -- 415bool(true) 416float(%d) 417string(6) "double" 418-- Iteration 14 -- 4198: Object of class point could not be converted to float 420bool(true) 421float(1) 422string(6) "double" 423 424-- Setting type of data to boolean -- 425-- Iteration 1 -- 426bool(true) 427bool(true) 428string(7) "boolean" 429-- Iteration 2 -- 430bool(true) 431bool(true) 432string(7) "boolean" 433-- Iteration 3 -- 434bool(true) 435bool(true) 436string(7) "boolean" 437-- Iteration 4 -- 438bool(true) 439bool(true) 440string(7) "boolean" 441-- Iteration 5 -- 442bool(true) 443bool(true) 444string(7) "boolean" 445-- Iteration 6 -- 446bool(true) 447bool(true) 448string(7) "boolean" 449-- Iteration 7 -- 450bool(true) 451bool(true) 452string(7) "boolean" 453-- Iteration 8 -- 454bool(true) 455bool(false) 456string(7) "boolean" 457-- Iteration 9 -- 458bool(true) 459bool(false) 460string(7) "boolean" 461-- Iteration 10 -- 462bool(true) 463bool(true) 464string(7) "boolean" 465-- Iteration 11 -- 466bool(true) 467bool(true) 468string(7) "boolean" 469-- Iteration 12 -- 470bool(true) 471bool(true) 472string(7) "boolean" 473-- Iteration 13 -- 474bool(true) 475bool(true) 476string(7) "boolean" 477-- Iteration 14 -- 478bool(true) 479bool(true) 480string(7) "boolean" 481 482-- Setting type of data to bool -- 483-- Iteration 1 -- 484bool(true) 485bool(true) 486string(7) "boolean" 487-- Iteration 2 -- 488bool(true) 489bool(true) 490string(7) "boolean" 491-- Iteration 3 -- 492bool(true) 493bool(true) 494string(7) "boolean" 495-- Iteration 4 -- 496bool(true) 497bool(true) 498string(7) "boolean" 499-- Iteration 5 -- 500bool(true) 501bool(true) 502string(7) "boolean" 503-- Iteration 6 -- 504bool(true) 505bool(true) 506string(7) "boolean" 507-- Iteration 7 -- 508bool(true) 509bool(true) 510string(7) "boolean" 511-- Iteration 8 -- 512bool(true) 513bool(false) 514string(7) "boolean" 515-- Iteration 9 -- 516bool(true) 517bool(false) 518string(7) "boolean" 519-- Iteration 10 -- 520bool(true) 521bool(true) 522string(7) "boolean" 523-- Iteration 11 -- 524bool(true) 525bool(true) 526string(7) "boolean" 527-- Iteration 12 -- 528bool(true) 529bool(true) 530string(7) "boolean" 531-- Iteration 13 -- 532bool(true) 533bool(true) 534string(7) "boolean" 535-- Iteration 14 -- 536bool(true) 537bool(true) 538string(7) "boolean" 539 540-- Setting type of data to resource -- 541-- Iteration 1 -- 5422: settype(): Cannot convert to resource type 543bool(false) 544array(3) { 545 [0]=> 546 int(1) 547 [1]=> 548 int(2) 549 [2]=> 550 int(3) 551} 552string(5) "array" 553-- Iteration 2 -- 5542: settype(): Cannot convert to resource type 555bool(false) 556string(14) "another string" 557string(6) "string" 558-- Iteration 3 -- 5592: settype(): Cannot convert to resource type 560bool(false) 561array(3) { 562 [0]=> 563 int(2) 564 [1]=> 565 int(3) 566 [2]=> 567 int(4) 568} 569string(5) "array" 570-- Iteration 4 -- 5712: settype(): Cannot convert to resource type 572bool(false) 573int(1) 574string(7) "integer" 575-- Iteration 5 -- 5762: settype(): Cannot convert to resource type 577bool(false) 578int(-20) 579string(7) "integer" 580-- Iteration 6 -- 5812: settype(): Cannot convert to resource type 582bool(false) 583float(2.54) 584string(6) "double" 585-- Iteration 7 -- 5862: settype(): Cannot convert to resource type 587bool(false) 588float(-2.54) 589string(6) "double" 590-- Iteration 8 -- 5912: settype(): Cannot convert to resource type 592bool(false) 593NULL 594string(4) "NULL" 595-- Iteration 9 -- 5962: settype(): Cannot convert to resource type 597bool(false) 598bool(false) 599string(7) "boolean" 600-- Iteration 10 -- 6012: settype(): Cannot convert to resource type 602bool(false) 603string(11) "some string" 604string(6) "string" 605-- Iteration 11 -- 6062: settype(): Cannot convert to resource type 607bool(false) 608string(6) "string" 609string(6) "string" 610-- Iteration 12 -- 6112: settype(): Cannot convert to resource type 612bool(false) 613resource(%d) of type (stream) 614string(8) "resource" 615-- Iteration 13 -- 6162: settype(): Cannot convert to resource type 617bool(false) 618resource(%d) of type (stream) 619string(8) "resource" 620-- Iteration 14 -- 6212: settype(): Cannot convert to resource type 622bool(false) 623object(point)#1 (2) { 624 ["x"]=> 625 int(10) 626 ["y"]=> 627 int(20) 628} 629string(6) "object" 630 631-- Setting type of data to array -- 632-- Iteration 1 -- 633bool(true) 634array(3) { 635 [0]=> 636 int(1) 637 [1]=> 638 int(2) 639 [2]=> 640 int(3) 641} 642string(5) "array" 643-- Iteration 2 -- 644bool(true) 645array(1) { 646 [0]=> 647 string(14) "another string" 648} 649string(5) "array" 650-- Iteration 3 -- 651bool(true) 652array(3) { 653 [0]=> 654 int(2) 655 [1]=> 656 int(3) 657 [2]=> 658 int(4) 659} 660string(5) "array" 661-- Iteration 4 -- 662bool(true) 663array(1) { 664 [0]=> 665 int(1) 666} 667string(5) "array" 668-- Iteration 5 -- 669bool(true) 670array(1) { 671 [0]=> 672 int(-20) 673} 674string(5) "array" 675-- Iteration 6 -- 676bool(true) 677array(1) { 678 [0]=> 679 float(2.54) 680} 681string(5) "array" 682-- Iteration 7 -- 683bool(true) 684array(1) { 685 [0]=> 686 float(-2.54) 687} 688string(5) "array" 689-- Iteration 8 -- 690bool(true) 691array(0) { 692} 693string(5) "array" 694-- Iteration 9 -- 695bool(true) 696array(1) { 697 [0]=> 698 bool(false) 699} 700string(5) "array" 701-- Iteration 10 -- 702bool(true) 703array(1) { 704 [0]=> 705 string(11) "some string" 706} 707string(5) "array" 708-- Iteration 11 -- 709bool(true) 710array(1) { 711 [0]=> 712 string(6) "string" 713} 714string(5) "array" 715-- Iteration 12 -- 716bool(true) 717array(1) { 718 [0]=> 719 resource(%d) of type (stream) 720} 721string(5) "array" 722-- Iteration 13 -- 723bool(true) 724array(1) { 725 [0]=> 726 resource(%d) of type (stream) 727} 728string(5) "array" 729-- Iteration 14 -- 730bool(true) 731array(2) { 732 ["x"]=> 733 int(10) 734 ["y"]=> 735 int(20) 736} 737string(5) "array" 738 739-- Setting type of data to object -- 740-- Iteration 1 -- 741bool(true) 742object(stdClass)#2 (3) { 743 [0]=> 744 int(1) 745 [1]=> 746 int(2) 747 [2]=> 748 int(3) 749} 750string(6) "object" 751-- Iteration 2 -- 752bool(true) 753object(stdClass)#2 (1) { 754 ["scalar"]=> 755 string(14) "another string" 756} 757string(6) "object" 758-- Iteration 3 -- 759bool(true) 760object(stdClass)#2 (3) { 761 [0]=> 762 int(2) 763 [1]=> 764 int(3) 765 [2]=> 766 int(4) 767} 768string(6) "object" 769-- Iteration 4 -- 770bool(true) 771object(stdClass)#2 (1) { 772 ["scalar"]=> 773 int(1) 774} 775string(6) "object" 776-- Iteration 5 -- 777bool(true) 778object(stdClass)#2 (1) { 779 ["scalar"]=> 780 int(-20) 781} 782string(6) "object" 783-- Iteration 6 -- 784bool(true) 785object(stdClass)#2 (1) { 786 ["scalar"]=> 787 float(2.54) 788} 789string(6) "object" 790-- Iteration 7 -- 791bool(true) 792object(stdClass)#2 (1) { 793 ["scalar"]=> 794 float(-2.54) 795} 796string(6) "object" 797-- Iteration 8 -- 798bool(true) 799object(stdClass)#2 (0) { 800} 801string(6) "object" 802-- Iteration 9 -- 803bool(true) 804object(stdClass)#2 (1) { 805 ["scalar"]=> 806 bool(false) 807} 808string(6) "object" 809-- Iteration 10 -- 810bool(true) 811object(stdClass)#2 (1) { 812 ["scalar"]=> 813 string(11) "some string" 814} 815string(6) "object" 816-- Iteration 11 -- 817bool(true) 818object(stdClass)#2 (1) { 819 ["scalar"]=> 820 string(6) "string" 821} 822string(6) "object" 823-- Iteration 12 -- 824bool(true) 825object(stdClass)#2 (1) { 826 ["scalar"]=> 827 resource(%d) of type (stream) 828} 829string(6) "object" 830-- Iteration 13 -- 831bool(true) 832object(stdClass)#2 (1) { 833 ["scalar"]=> 834 resource(%d) of type (stream) 835} 836string(6) "object" 837-- Iteration 14 -- 838bool(true) 839object(point)#1 (2) { 840 ["x"]=> 841 int(10) 842 ["y"]=> 843 int(20) 844} 845string(6) "object" 846 847-- Setting type of data to string -- 848-- Iteration 1 -- 8498: Array to string conversion 850bool(true) 851string(5) "Array" 852string(6) "string" 853-- Iteration 2 -- 854bool(true) 855string(14) "another string" 856string(6) "string" 857-- Iteration 3 -- 8588: Array to string conversion 859bool(true) 860string(5) "Array" 861string(6) "string" 862-- Iteration 4 -- 863bool(true) 864string(1) "1" 865string(6) "string" 866-- Iteration 5 -- 867bool(true) 868string(3) "-20" 869string(6) "string" 870-- Iteration 6 -- 871bool(true) 872string(4) "2.54" 873string(6) "string" 874-- Iteration 7 -- 875bool(true) 876string(5) "-2.54" 877string(6) "string" 878-- Iteration 8 -- 879bool(true) 880string(0) "" 881string(6) "string" 882-- Iteration 9 -- 883bool(true) 884string(0) "" 885string(6) "string" 886-- Iteration 10 -- 887bool(true) 888string(11) "some string" 889string(6) "string" 890-- Iteration 11 -- 891bool(true) 892string(6) "string" 893string(6) "string" 894-- Iteration 12 -- 895bool(true) 896string(14) "Resource id #%d" 897string(6) "string" 898-- Iteration 13 -- 899bool(true) 900string(14) "Resource id #%d" 901string(6) "string" 902-- Iteration 14 -- 903bool(true) 904string(6) "Object" 905string(6) "string" 906Done 907