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