1--TEST--
2Test intval() function
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only");
6?>
7--FILE--
8<?php
9/* Prototype: int intval( mixed $var [.int $base] );
10 * Description: Returns the integer value of var, using the specified base for the conversion(the default is base 10).
11 */
12
13echo "*** Testing intval() with valid integer values ***\n";
14// different valid  integer values
15$valid_ints = array(
16                '0',
17                '1',
18                '-1',
19                '-2147483648', // max negative integer value
20                '-2147483647',
21                2147483647,  // max positive integer value
22                2147483640,
23                0x123B,      // integer as hexadecimal
24                '0x12ab',
25                '0Xfff',
26                '0XFA',
27                -0x80000000, // max negative integer as hexadecimal
28                '0x7fffffff',  // max positive integer as hexadecimal
29                0x7FFFFFFF,  // max positive integer as hexadecimal
30                '0123',        // integer as octal
31                01,       // should be quivalent to octal 1
32                -020000000000, // max negative integer as octal
33                017777777777,  // max positive integer as octal
34               );
35
36/* loop to check that intval() recognizes different
37   integer values, expected output:integer value in decimal notation for valid integer */
38
39echo "\n***Output with default base value ie 10 ***\n";
40foreach ($valid_ints as $value ) {
41   var_dump( intval($value) );
42}
43
44
45echo "\n***Output with base value of 10( explicitly passed as argument) ***\n";
46foreach ($valid_ints as $value ) {
47   var_dump( intval($value, 10) );
48}
49
50
51echo "\n***Output with base value  of 16 ***\n";
52foreach ($valid_ints as $value ) {
53   var_dump( intval($value, 16) );
54}
55
56echo "\n***Output with base value of 8 ***\n";
57foreach ($valid_ints as $value ) {
58   var_dump( intval($value, 8) );
59}
60
61echo "\n*** Testing intval() on non integer types ***\n";
62
63// get a resource type variable
64$fp = fopen (__FILE__, "r");
65fclose($fp);
66$dfp = opendir ( __DIR__ );
67closedir($dfp);
68
69// unset variable
70
71$unset_var = 10;
72unset ($unset_var);
73
74// other types in a array
75$not_int_types = array (
76  /* float values */
77  '-2147483649', // float value
78  '2147483648',  // float value
79  '-0x80000001', // float value, beyond max negative int
80  '0x800000001', // float value, beyond max positive int
81  '020000000001', // float value, beyond max positive int
82  '-020000000001', // float value, beyond max negative int
83  0.0,
84  -0.1,
85  1.0,
86  1e5,
87  -1e6,
88  1E8,
89  -1E9,
90  10.0000000000000000005,
91  10.5e+5,
92
93  /* resources */
94  $fp,
95  $dfp,
96
97  /* arrays */
98  array(),
99  array(0),
100  array(1),
101  array(NULL),
102  array(null),
103  array("string"),
104  array(true),
105  array(TRUE),
106  array(false),
107  array(FALSE),
108  array(1,2,3,4),
109  array(1 => "One", "two" => 2),
110
111  /* strings */
112  "",
113  '',
114  "0",
115  '0',
116  "1",
117  '1',
118  "\x01",
119  '\x01',
120  "\01",
121  '\01',
122  'string',
123  "string",
124  "true",
125  "FALSE",
126  'false',
127  'TRUE',
128  "NULL",
129  'null',
130
131  /* booleans */
132  true,
133  false,
134  TRUE,
135  FALSE,
136
137  /* undefined and unset vars */
138  @$unset_var,
139  @$undefined_var
140);
141
142
143/* loop through the $not_int_types to see working of
144   intval() on non integer types, expected output: integer value in decimal notation for valid integers */
145foreach ($not_int_types as $type ) {
146   var_dump( intval($type) );
147}
148
149echo "\n*** Testing error conditions ***\n";
150//Zero argument
151var_dump( intval() );
152
153//arguments more than expected
154var_dump( intval(TRUE, FALSE, TRUE) );
155
156echo "\n--- Done ---\n";
157
158
159?>
160--EXPECTF--
161*** Testing intval() with valid integer values ***
162
163***Output with default base value ie 10 ***
164int(0)
165int(1)
166int(-1)
167int(-2147483648)
168int(-2147483647)
169int(2147483647)
170int(2147483640)
171int(4667)
172int(0)
173int(0)
174int(0)
175int(-2147483648)
176int(0)
177int(2147483647)
178int(123)
179int(1)
180int(-2147483648)
181int(2147483647)
182
183***Output with base value of 10( explicitly passed as argument) ***
184int(0)
185int(1)
186int(-1)
187int(-2147483648)
188int(-2147483647)
189int(2147483647)
190int(2147483640)
191int(4667)
192int(0)
193int(0)
194int(0)
195int(-2147483648)
196int(0)
197int(2147483647)
198int(123)
199int(1)
200int(-2147483648)
201int(2147483647)
202
203***Output with base value  of 16 ***
204int(0)
205int(1)
206int(-1)
207int(-2147483648)
208int(-2147483648)
209int(2147483647)
210int(2147483640)
211int(4667)
212int(4779)
213int(4095)
214int(250)
215int(-2147483648)
216int(2147483647)
217int(2147483647)
218int(291)
219int(1)
220int(-2147483648)
221int(2147483647)
222
223***Output with base value of 8 ***
224int(0)
225int(1)
226int(-1)
227int(-9020)
228int(-9020)
229int(2147483647)
230int(2147483640)
231int(4667)
232int(0)
233int(0)
234int(0)
235int(-2147483648)
236int(0)
237int(2147483647)
238int(83)
239int(1)
240int(-2147483648)
241int(2147483647)
242
243*** Testing intval() on non integer types ***
244int(-2147483648)
245int(2147483647)
246int(0)
247int(0)
248int(2147483647)
249int(-2147483648)
250int(0)
251int(0)
252int(1)
253int(100000)
254int(-1000000)
255int(100000000)
256int(-1000000000)
257int(10)
258int(1050000)
259int(%d)
260int(%d)
261int(0)
262int(1)
263int(1)
264int(1)
265int(1)
266int(1)
267int(1)
268int(1)
269int(1)
270int(1)
271int(1)
272int(1)
273int(0)
274int(0)
275int(0)
276int(0)
277int(1)
278int(1)
279int(0)
280int(0)
281int(0)
282int(0)
283int(0)
284int(0)
285int(0)
286int(0)
287int(0)
288int(0)
289int(0)
290int(0)
291int(1)
292int(0)
293int(1)
294int(0)
295int(0)
296int(0)
297
298*** Testing error conditions ***
299
300Warning: intval() expects at least 1 parameter, 0 given in %s on line %d
301NULL
302
303Warning: intval() expects at most 2 parameters, 3 given in %s on line %d
304NULL
305
306--- Done ---
307