1--TEST--
2gettype(), settype() and friends
3--FILE--
4<?php
5
6function foo($errno, $errstr, $errfile, $errline) {
7    var_dump($errstr);
8}
9
10set_error_handler("foo");
11
12$fp = fopen(__FILE__, "r");
13fclose($fp);
14$fp1 = fopen(__FILE__, "r");
15
16$var1 = "another string";
17$var2 = array(2,3,4);
18
19$array = array(
20    array(1,2,3),
21    $var1,
22    $var2,
23    1,
24    2.0,
25    NULL,
26    false,
27    "some string",
28    $fp,
29    $fp1,
30    new stdclass,
31);
32
33$types = array(
34    "null",
35    "integer",
36    "double",
37    "boolean",
38    "resource",
39    "array",
40    "object",
41    "string"
42    );
43
44foreach ($array as $var) {
45    var_dump(gettype($var));
46}
47
48foreach ($types as $type) {
49    foreach ($array as $var) {
50        try {
51            var_dump(settype($var, $type));
52        } catch (Error $e) {
53            echo "Error: ", $e->getMessage(), "\n";
54        }
55        var_dump($var);
56    }
57}
58
59echo "Done\n";
60?>
61--EXPECTF--
62string(5) "array"
63string(6) "string"
64string(5) "array"
65string(7) "integer"
66string(6) "double"
67string(4) "NULL"
68string(7) "boolean"
69string(6) "string"
70string(17) "resource (closed)"
71string(8) "resource"
72string(6) "object"
73bool(true)
74NULL
75bool(true)
76NULL
77bool(true)
78NULL
79bool(true)
80NULL
81bool(true)
82NULL
83bool(true)
84NULL
85bool(true)
86NULL
87bool(true)
88NULL
89bool(true)
90NULL
91bool(true)
92NULL
93bool(true)
94NULL
95bool(true)
96int(1)
97bool(true)
98int(0)
99bool(true)
100int(1)
101bool(true)
102int(1)
103bool(true)
104int(2)
105bool(true)
106int(0)
107bool(true)
108int(0)
109bool(true)
110int(0)
111bool(true)
112int(%d)
113bool(true)
114int(%d)
115string(54) "Object of class stdClass could not be converted to int"
116bool(true)
117int(%d)
118bool(true)
119float(1)
120bool(true)
121float(0)
122bool(true)
123float(1)
124bool(true)
125float(1)
126bool(true)
127float(2)
128bool(true)
129float(0)
130bool(true)
131float(0)
132bool(true)
133float(0)
134bool(true)
135float(%f)
136bool(true)
137float(%f)
138string(56) "Object of class stdClass could not be converted to float"
139bool(true)
140float(%f)
141bool(true)
142bool(true)
143bool(true)
144bool(true)
145bool(true)
146bool(true)
147bool(true)
148bool(true)
149bool(true)
150bool(true)
151bool(true)
152bool(false)
153bool(true)
154bool(false)
155bool(true)
156bool(true)
157bool(true)
158bool(true)
159bool(true)
160bool(true)
161bool(true)
162bool(true)
163Error: Cannot convert to resource type
164array(3) {
165  [0]=>
166  int(1)
167  [1]=>
168  int(2)
169  [2]=>
170  int(3)
171}
172Error: Cannot convert to resource type
173string(14) "another string"
174Error: Cannot convert to resource type
175array(3) {
176  [0]=>
177  int(2)
178  [1]=>
179  int(3)
180  [2]=>
181  int(4)
182}
183Error: Cannot convert to resource type
184int(1)
185Error: Cannot convert to resource type
186float(2)
187Error: Cannot convert to resource type
188NULL
189Error: Cannot convert to resource type
190bool(false)
191Error: Cannot convert to resource type
192string(11) "some string"
193Error: Cannot convert to resource type
194resource(%d) of type (Unknown)
195Error: Cannot convert to resource type
196resource(%d) of type (stream)
197Error: Cannot convert to resource type
198object(stdClass)#%d (0) {
199}
200bool(true)
201array(3) {
202  [0]=>
203  int(1)
204  [1]=>
205  int(2)
206  [2]=>
207  int(3)
208}
209bool(true)
210array(1) {
211  [0]=>
212  string(14) "another string"
213}
214bool(true)
215array(3) {
216  [0]=>
217  int(2)
218  [1]=>
219  int(3)
220  [2]=>
221  int(4)
222}
223bool(true)
224array(1) {
225  [0]=>
226  int(1)
227}
228bool(true)
229array(1) {
230  [0]=>
231  float(2)
232}
233bool(true)
234array(0) {
235}
236bool(true)
237array(1) {
238  [0]=>
239  bool(false)
240}
241bool(true)
242array(1) {
243  [0]=>
244  string(11) "some string"
245}
246bool(true)
247array(1) {
248  [0]=>
249  resource(%d) of type (Unknown)
250}
251bool(true)
252array(1) {
253  [0]=>
254  resource(%d) of type (stream)
255}
256bool(true)
257array(0) {
258}
259bool(true)
260object(stdClass)#%d (3) {
261  ["0"]=>
262  int(1)
263  ["1"]=>
264  int(2)
265  ["2"]=>
266  int(3)
267}
268bool(true)
269object(stdClass)#%d (1) {
270  ["scalar"]=>
271  string(14) "another string"
272}
273bool(true)
274object(stdClass)#%d (3) {
275  ["0"]=>
276  int(2)
277  ["1"]=>
278  int(3)
279  ["2"]=>
280  int(4)
281}
282bool(true)
283object(stdClass)#%d (1) {
284  ["scalar"]=>
285  int(1)
286}
287bool(true)
288object(stdClass)#%d (1) {
289  ["scalar"]=>
290  float(2)
291}
292bool(true)
293object(stdClass)#%d (0) {
294}
295bool(true)
296object(stdClass)#%d (1) {
297  ["scalar"]=>
298  bool(false)
299}
300bool(true)
301object(stdClass)#%d (1) {
302  ["scalar"]=>
303  string(11) "some string"
304}
305bool(true)
306object(stdClass)#%d (1) {
307  ["scalar"]=>
308  resource(%d) of type (Unknown)
309}
310bool(true)
311object(stdClass)#%d (1) {
312  ["scalar"]=>
313  resource(%d) of type (stream)
314}
315bool(true)
316object(stdClass)#%d (0) {
317}
318string(26) "Array to string conversion"
319bool(true)
320string(5) "Array"
321bool(true)
322string(14) "another string"
323string(26) "Array to string conversion"
324bool(true)
325string(5) "Array"
326bool(true)
327string(1) "1"
328bool(true)
329string(1) "2"
330bool(true)
331string(0) ""
332bool(true)
333string(0) ""
334bool(true)
335string(11) "some string"
336bool(true)
337string(14) "Resource id #%d"
338bool(true)
339string(14) "Resource id #%d"
340Error: Object of class stdClass could not be converted to string
341string(0) ""
342Done
343