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