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/* Test usage variation of gettype() and settype() functions: 12 settype() to resource type. 13 Set type of the data to "resource" and verify using gettype 14 Following are performed in the listed sequence: 15 get the current type of the variable 16 set the type of the variable to resource type 17 dump the variable to see its new data 18 get the new type of the variable 19*/ 20$var1 = "another string"; 21$var2 = array(2,3,4); 22 23class point 24{ 25 var $x; 26 var $y; 27 28 function __construct($x, $y) { 29 $this->x = $x; 30 $this->y = $y; 31 } 32 33 function __toString() { 34 return "ObjectPoint"; 35 } 36} 37 38$var_values = array ( 39 /* nulls */ 40 null, 41 42 /* boolean */ 43 FALSE, 44 TRUE, 45 true, 46 47 /* strings */ 48 "\$", 49 "\x66", 50 "\0123", 51 "", 52 '', 53 " ", 54 ' ', 55 /* numerics in the form of string */ 56 '10', 57 "10", 58 "10string", 59 '10string', 60 "1", 61 "-1", 62 "1e2", 63 " 1", 64 "2974394749328742328432", 65 "-1e-2", 66 '1', 67 '-1', 68 '1e2', 69 ' 1', 70 '2974394749328742328432', 71 '-1e-2', 72 "0xff", 73 '0x55', 74 '0XA55', 75 '0X123', 76 "0123", 77 '0123', 78 "-0123", 79 "+0123", 80 '-0123', 81 '+0123', 82 "-0x80001", // invalid numerics as its prefix with sign or have decimal points 83 "+0x80001", 84 "-0x80001.5", 85 "0x80001.5", 86 "@$%#$%^$%^&^", 87 88 /* arrays */ 89 array(), 90 array(NULL), 91 array(1,2,3,4), 92 array(1 => "one", 2 => "two", "3" => "three", "four" => 4), 93 array(1.5, 2.4, 6.5e6), 94 95 /* integers */ 96 -2147483648, // max -ne int value 97 2147483647, 98 2147483649, 99 1232147483649, 100 0x55, 101 0xF674593039, // a hex value > than max int 102 -0X558F, 103 0555, 104 -0555, 105 02224242434343152, // an octal value > than max int 106 107 /* floats */ 108 1e5, 109 -1e5, 110 1E5, 111 -1E5, 112 -1.5, 113 .5, 114 -.5, 115 .5e6, 116 -.5e6, 117 -.5e-6, 118 .5e+6, 119 -.5e+6, 120 .512E6, 121 -.512E6, 122 .512E-6, 123 +.512E-6, 124 .512E+6, 125 -.512E+6, 126 127 new point(NULL, NULL), 128 new point(2.5, 40.5), 129 new point(0, 0), 130); 131 132/* test conversion to resource type */ 133$type = "resource"; 134 135echo "\n*** Testing gettype() & settype() functions : usage variations ***\n"; 136echo "\n-- Setting type of data to $type --\n"; 137$loop_count = 1; 138foreach ($var_values as $var) { 139 echo "-- Iteration $loop_count --\n"; $loop_count++; 140 141 // get the current data type 142 var_dump( gettype($var) ); 143 144 // convert it to resource 145 try { 146 var_dump(settype($var, $type)); 147 } catch (ValueError $exception) { 148 echo $exception->getMessage() . "\n"; 149 } 150 151 // dump the converted data 152 var_dump($var); 153 154 // check the new type after conversion 155 var_dump(gettype($var)); 156} 157 158echo "Done\n"; 159?> 160--EXPECT-- 161*** Testing gettype() & settype() functions : usage variations *** 162 163-- Setting type of data to resource -- 164-- Iteration 1 -- 165string(4) "NULL" 166Cannot convert to resource type 167NULL 168string(4) "NULL" 169-- Iteration 2 -- 170string(7) "boolean" 171Cannot convert to resource type 172bool(false) 173string(7) "boolean" 174-- Iteration 3 -- 175string(7) "boolean" 176Cannot convert to resource type 177bool(true) 178string(7) "boolean" 179-- Iteration 4 -- 180string(7) "boolean" 181Cannot convert to resource type 182bool(true) 183string(7) "boolean" 184-- Iteration 5 -- 185string(6) "string" 186Cannot convert to resource type 187string(1) "$" 188string(6) "string" 189-- Iteration 6 -- 190string(6) "string" 191Cannot convert to resource type 192string(1) "f" 193string(6) "string" 194-- Iteration 7 -- 195string(6) "string" 196Cannot convert to resource type 197string(2) " 1983" 199string(6) "string" 200-- Iteration 8 -- 201string(6) "string" 202Cannot convert to resource type 203string(0) "" 204string(6) "string" 205-- Iteration 9 -- 206string(6) "string" 207Cannot convert to resource type 208string(0) "" 209string(6) "string" 210-- Iteration 10 -- 211string(6) "string" 212Cannot convert to resource type 213string(1) " " 214string(6) "string" 215-- Iteration 11 -- 216string(6) "string" 217Cannot convert to resource type 218string(1) " " 219string(6) "string" 220-- Iteration 12 -- 221string(6) "string" 222Cannot convert to resource type 223string(2) "10" 224string(6) "string" 225-- Iteration 13 -- 226string(6) "string" 227Cannot convert to resource type 228string(2) "10" 229string(6) "string" 230-- Iteration 14 -- 231string(6) "string" 232Cannot convert to resource type 233string(8) "10string" 234string(6) "string" 235-- Iteration 15 -- 236string(6) "string" 237Cannot convert to resource type 238string(8) "10string" 239string(6) "string" 240-- Iteration 16 -- 241string(6) "string" 242Cannot convert to resource type 243string(1) "1" 244string(6) "string" 245-- Iteration 17 -- 246string(6) "string" 247Cannot convert to resource type 248string(2) "-1" 249string(6) "string" 250-- Iteration 18 -- 251string(6) "string" 252Cannot convert to resource type 253string(3) "1e2" 254string(6) "string" 255-- Iteration 19 -- 256string(6) "string" 257Cannot convert to resource type 258string(2) " 1" 259string(6) "string" 260-- Iteration 20 -- 261string(6) "string" 262Cannot convert to resource type 263string(22) "2974394749328742328432" 264string(6) "string" 265-- Iteration 21 -- 266string(6) "string" 267Cannot convert to resource type 268string(5) "-1e-2" 269string(6) "string" 270-- Iteration 22 -- 271string(6) "string" 272Cannot convert to resource type 273string(1) "1" 274string(6) "string" 275-- Iteration 23 -- 276string(6) "string" 277Cannot convert to resource type 278string(2) "-1" 279string(6) "string" 280-- Iteration 24 -- 281string(6) "string" 282Cannot convert to resource type 283string(3) "1e2" 284string(6) "string" 285-- Iteration 25 -- 286string(6) "string" 287Cannot convert to resource type 288string(2) " 1" 289string(6) "string" 290-- Iteration 26 -- 291string(6) "string" 292Cannot convert to resource type 293string(22) "2974394749328742328432" 294string(6) "string" 295-- Iteration 27 -- 296string(6) "string" 297Cannot convert to resource type 298string(5) "-1e-2" 299string(6) "string" 300-- Iteration 28 -- 301string(6) "string" 302Cannot convert to resource type 303string(4) "0xff" 304string(6) "string" 305-- Iteration 29 -- 306string(6) "string" 307Cannot convert to resource type 308string(4) "0x55" 309string(6) "string" 310-- Iteration 30 -- 311string(6) "string" 312Cannot convert to resource type 313string(5) "0XA55" 314string(6) "string" 315-- Iteration 31 -- 316string(6) "string" 317Cannot convert to resource type 318string(5) "0X123" 319string(6) "string" 320-- Iteration 32 -- 321string(6) "string" 322Cannot convert to resource type 323string(4) "0123" 324string(6) "string" 325-- Iteration 33 -- 326string(6) "string" 327Cannot convert to resource type 328string(4) "0123" 329string(6) "string" 330-- Iteration 34 -- 331string(6) "string" 332Cannot convert to resource type 333string(5) "-0123" 334string(6) "string" 335-- Iteration 35 -- 336string(6) "string" 337Cannot convert to resource type 338string(5) "+0123" 339string(6) "string" 340-- Iteration 36 -- 341string(6) "string" 342Cannot convert to resource type 343string(5) "-0123" 344string(6) "string" 345-- Iteration 37 -- 346string(6) "string" 347Cannot convert to resource type 348string(5) "+0123" 349string(6) "string" 350-- Iteration 38 -- 351string(6) "string" 352Cannot convert to resource type 353string(8) "-0x80001" 354string(6) "string" 355-- Iteration 39 -- 356string(6) "string" 357Cannot convert to resource type 358string(8) "+0x80001" 359string(6) "string" 360-- Iteration 40 -- 361string(6) "string" 362Cannot convert to resource type 363string(10) "-0x80001.5" 364string(6) "string" 365-- Iteration 41 -- 366string(6) "string" 367Cannot convert to resource type 368string(9) "0x80001.5" 369string(6) "string" 370-- Iteration 42 -- 371string(6) "string" 372Cannot convert to resource type 373string(12) "@$%#$%^$%^&^" 374string(6) "string" 375-- Iteration 43 -- 376string(5) "array" 377Cannot convert to resource type 378array(0) { 379} 380string(5) "array" 381-- Iteration 44 -- 382string(5) "array" 383Cannot convert to resource type 384array(1) { 385 [0]=> 386 NULL 387} 388string(5) "array" 389-- Iteration 45 -- 390string(5) "array" 391Cannot convert to resource type 392array(4) { 393 [0]=> 394 int(1) 395 [1]=> 396 int(2) 397 [2]=> 398 int(3) 399 [3]=> 400 int(4) 401} 402string(5) "array" 403-- Iteration 46 -- 404string(5) "array" 405Cannot convert to resource type 406array(4) { 407 [1]=> 408 string(3) "one" 409 [2]=> 410 string(3) "two" 411 [3]=> 412 string(5) "three" 413 ["four"]=> 414 int(4) 415} 416string(5) "array" 417-- Iteration 47 -- 418string(5) "array" 419Cannot convert to resource type 420array(3) { 421 [0]=> 422 float(1.5) 423 [1]=> 424 float(2.4) 425 [2]=> 426 float(6500000) 427} 428string(5) "array" 429-- Iteration 48 -- 430string(6) "double" 431Cannot convert to resource type 432float(-2147483648) 433string(6) "double" 434-- Iteration 49 -- 435string(7) "integer" 436Cannot convert to resource type 437int(2147483647) 438string(7) "integer" 439-- Iteration 50 -- 440string(6) "double" 441Cannot convert to resource type 442float(2147483649) 443string(6) "double" 444-- Iteration 51 -- 445string(6) "double" 446Cannot convert to resource type 447float(1232147483649) 448string(6) "double" 449-- Iteration 52 -- 450string(7) "integer" 451Cannot convert to resource type 452int(85) 453string(7) "integer" 454-- Iteration 53 -- 455string(6) "double" 456Cannot convert to resource type 457float(1058513956921) 458string(6) "double" 459-- Iteration 54 -- 460string(7) "integer" 461Cannot convert to resource type 462int(-21903) 463string(7) "integer" 464-- Iteration 55 -- 465string(7) "integer" 466Cannot convert to resource type 467int(365) 468string(7) "integer" 469-- Iteration 56 -- 470string(7) "integer" 471Cannot convert to resource type 472int(-365) 473string(7) "integer" 474-- Iteration 57 -- 475string(6) "double" 476Cannot convert to resource type 477float(80561044571754) 478string(6) "double" 479-- Iteration 58 -- 480string(6) "double" 481Cannot convert to resource type 482float(100000) 483string(6) "double" 484-- Iteration 59 -- 485string(6) "double" 486Cannot convert to resource type 487float(-100000) 488string(6) "double" 489-- Iteration 60 -- 490string(6) "double" 491Cannot convert to resource type 492float(100000) 493string(6) "double" 494-- Iteration 61 -- 495string(6) "double" 496Cannot convert to resource type 497float(-100000) 498string(6) "double" 499-- Iteration 62 -- 500string(6) "double" 501Cannot convert to resource type 502float(-1.5) 503string(6) "double" 504-- Iteration 63 -- 505string(6) "double" 506Cannot convert to resource type 507float(0.5) 508string(6) "double" 509-- Iteration 64 -- 510string(6) "double" 511Cannot convert to resource type 512float(-0.5) 513string(6) "double" 514-- Iteration 65 -- 515string(6) "double" 516Cannot convert to resource type 517float(500000) 518string(6) "double" 519-- Iteration 66 -- 520string(6) "double" 521Cannot convert to resource type 522float(-500000) 523string(6) "double" 524-- Iteration 67 -- 525string(6) "double" 526Cannot convert to resource type 527float(-5.0E-7) 528string(6) "double" 529-- Iteration 68 -- 530string(6) "double" 531Cannot convert to resource type 532float(500000) 533string(6) "double" 534-- Iteration 69 -- 535string(6) "double" 536Cannot convert to resource type 537float(-500000) 538string(6) "double" 539-- Iteration 70 -- 540string(6) "double" 541Cannot convert to resource type 542float(512000) 543string(6) "double" 544-- Iteration 71 -- 545string(6) "double" 546Cannot convert to resource type 547float(-512000) 548string(6) "double" 549-- Iteration 72 -- 550string(6) "double" 551Cannot convert to resource type 552float(5.12E-7) 553string(6) "double" 554-- Iteration 73 -- 555string(6) "double" 556Cannot convert to resource type 557float(5.12E-7) 558string(6) "double" 559-- Iteration 74 -- 560string(6) "double" 561Cannot convert to resource type 562float(512000) 563string(6) "double" 564-- Iteration 75 -- 565string(6) "double" 566Cannot convert to resource type 567float(-512000) 568string(6) "double" 569-- Iteration 76 -- 570string(6) "object" 571Cannot convert to resource type 572object(point)#1 (2) { 573 ["x"]=> 574 NULL 575 ["y"]=> 576 NULL 577} 578string(6) "object" 579-- Iteration 77 -- 580string(6) "object" 581Cannot convert to resource type 582object(point)#2 (2) { 583 ["x"]=> 584 float(2.5) 585 ["y"]=> 586 float(40.5) 587} 588string(6) "object" 589-- Iteration 78 -- 590string(6) "object" 591Cannot convert to resource type 592object(point)#3 (2) { 593 ["x"]=> 594 int(0) 595 ["y"]=> 596 int(0) 597} 598string(6) "object" 599Done 600