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