1--TEST--
2Test file() function : third parameter variation
3--FILE--
4<?php
5/* Prototype  : array file(string filename [, int flags[, resource context]])
6 * Description: Read entire file into an array
7 * Source code: ext/standard/file.c
8 * Alias to functions:
9 */
10
11echo "*** Testing file() : usage variation ***\n";
12
13// Define error handler
14function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
15	if (error_reporting() != 0) {
16		// report non-silenced errors
17		echo "Error: $err_no - $err_msg, $filename($linenum)\n";
18	}
19}
20set_error_handler('test_error_handler');
21
22// Initialise function arguments not being substituted
23$filename = __FILE__ . ".tmp";
24$fd = fopen($filename, "w+");
25fwrite($fd, "Line 1\nLine 2\nLine 3");
26fclose($fd);
27
28$flags = 0;
29
30//get an unset variable
31$unset_var = 10;
32unset ($unset_var);
33
34// define some classes
35class classWithToString
36{
37	public function __toString() {
38		return "Class A object";
39	}
40}
41
42class classWithoutToString
43{
44}
45
46// heredoc string
47$heredoc = <<<EOT
48hello world
49EOT;
50
51// add arrays
52$index_array = array (1, 2, 3);
53$assoc_array = array ('one' => 1, 'two' => 2);
54
55// create a file stream resource
56$tmp_filename = __FILE__ . ".tmp2";
57$file_stream_resource = fopen($tmp_filename, "w+");
58
59//array of values to iterate over
60$inputs = array(
61
62      // int data
63      'int 0' => 0,
64      'int 1' => 1,
65      'int 12345' => 12345,
66      'int -12345' => -2345,
67
68      // float data
69      'float 10.5' => 10.5,
70      'float -10.5' => -10.5,
71      'float 12.3456789000e10' => 12.3456789000e10,
72      'float -12.3456789000e10' => -12.3456789000e10,
73      'float .5' => .5,
74
75      // array data
76      'empty array' => array(),
77      'int indexed array' => $index_array,
78      'associative array' => $assoc_array,
79      'nested arrays' => array('foo', $index_array, $assoc_array),
80
81      // null data
82      'uppercase NULL' => NULL,
83      'lowercase null' => null,
84
85      // boolean data
86      'lowercase true' => true,
87      'lowercase false' =>false,
88      'uppercase TRUE' =>TRUE,
89      'uppercase FALSE' =>FALSE,
90
91      // empty data
92      'empty string DQ' => "",
93      'empty string SQ' => '',
94
95      // string data
96      'string DQ' => "string",
97      'string SQ' => 'string',
98      'mixed case string' => "sTrInG",
99      'heredoc' => $heredoc,
100
101      // object data
102      'instance of classWithToString' => new classWithToString(),
103      'instance of classWithoutToString' => new classWithoutToString(),
104
105      // undefined data
106      'undefined var' => @$undefined_var,
107
108      // unset data
109      'unset var' => @$unset_var,
110
111      // file stream resource
112      'file stream resource' => $file_stream_resource,
113);
114
115// loop through each element of the array for context
116
117foreach($inputs as $key =>$value) {
118      echo "\n--$key--\n";
119      var_dump( file($filename, $flags, $value) );
120};
121
122fclose($file_stream_resource);
123unlink($tmp_filename);
124unlink($filename);
125
126?>
127===DONE===
128--EXPECTF--
129*** Testing file() : usage variation ***
130
131--int 0--
132Error: 2 - file() expects parameter 3 to be resource, integer given, %s(%d)
133NULL
134
135--int 1--
136Error: 2 - file() expects parameter 3 to be resource, integer given, %s(%d)
137NULL
138
139--int 12345--
140Error: 2 - file() expects parameter 3 to be resource, integer given, %s(%d)
141NULL
142
143--int -12345--
144Error: 2 - file() expects parameter 3 to be resource, integer given, %s(%d)
145NULL
146
147--float 10.5--
148Error: 2 - file() expects parameter 3 to be resource, float given, %s(%d)
149NULL
150
151--float -10.5--
152Error: 2 - file() expects parameter 3 to be resource, float given, %s(%d)
153NULL
154
155--float 12.3456789000e10--
156Error: 2 - file() expects parameter 3 to be resource, float given, %s(%d)
157NULL
158
159--float -12.3456789000e10--
160Error: 2 - file() expects parameter 3 to be resource, float given, %s(%d)
161NULL
162
163--float .5--
164Error: 2 - file() expects parameter 3 to be resource, float given, %s(%d)
165NULL
166
167--empty array--
168Error: 2 - file() expects parameter 3 to be resource, array given, %s(%d)
169NULL
170
171--int indexed array--
172Error: 2 - file() expects parameter 3 to be resource, array given, %s(%d)
173NULL
174
175--associative array--
176Error: 2 - file() expects parameter 3 to be resource, array given, %s(%d)
177NULL
178
179--nested arrays--
180Error: 2 - file() expects parameter 3 to be resource, array given, %s(%d)
181NULL
182
183--uppercase NULL--
184array(3) {
185  [0]=>
186  string(7) "Line 1
187"
188  [1]=>
189  string(7) "Line 2
190"
191  [2]=>
192  string(6) "Line 3"
193}
194
195--lowercase null--
196array(3) {
197  [0]=>
198  string(7) "Line 1
199"
200  [1]=>
201  string(7) "Line 2
202"
203  [2]=>
204  string(6) "Line 3"
205}
206
207--lowercase true--
208Error: 2 - file() expects parameter 3 to be resource, boolean given, %s(%d)
209NULL
210
211--lowercase false--
212Error: 2 - file() expects parameter 3 to be resource, boolean given, %s(%d)
213NULL
214
215--uppercase TRUE--
216Error: 2 - file() expects parameter 3 to be resource, boolean given, %s(%d)
217NULL
218
219--uppercase FALSE--
220Error: 2 - file() expects parameter 3 to be resource, boolean given, %s(%d)
221NULL
222
223--empty string DQ--
224Error: 2 - file() expects parameter 3 to be resource, string given, %s(%d)
225NULL
226
227--empty string SQ--
228Error: 2 - file() expects parameter 3 to be resource, string given, %s(%d)
229NULL
230
231--string DQ--
232Error: 2 - file() expects parameter 3 to be resource, string given, %s(%d)
233NULL
234
235--string SQ--
236Error: 2 - file() expects parameter 3 to be resource, string given, %s(%d)
237NULL
238
239--mixed case string--
240Error: 2 - file() expects parameter 3 to be resource, string given, %s(%d)
241NULL
242
243--heredoc--
244Error: 2 - file() expects parameter 3 to be resource, string given, %s(%d)
245NULL
246
247--instance of classWithToString--
248Error: 2 - file() expects parameter 3 to be resource, object given, %s(%d)
249NULL
250
251--instance of classWithoutToString--
252Error: 2 - file() expects parameter 3 to be resource, object given, %s(%d)
253NULL
254
255--undefined var--
256array(3) {
257  [0]=>
258  string(7) "Line 1
259"
260  [1]=>
261  string(7) "Line 2
262"
263  [2]=>
264  string(6) "Line 3"
265}
266
267--unset var--
268array(3) {
269  [0]=>
270  string(7) "Line 1
271"
272  [1]=>
273  string(7) "Line 2
274"
275  [2]=>
276  string(6) "Line 3"
277}
278
279--file stream resource--
280Error: 2 - file(): supplied resource is not a valid Stream-Context resource, %s(%d)
281array(3) {
282  [0]=>
283  string(7) "Line 1
284"
285  [1]=>
286  string(7) "Line 2
287"
288  [2]=>
289  string(6) "Line 3"
290}
291===DONE===
292