1--TEST-- 2Test file() function : first 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$flags = 0; 24$context = stream_context_create(); 25 26 27//get an unset variable 28$unset_var = 10; 29unset ($unset_var); 30 31// define some classes 32class classWithToString 33{ 34 public function __toString() { 35 return "Class A object"; 36 } 37} 38 39class classWithoutToString 40{ 41} 42 43// heredoc string 44$heredoc = <<<EOT 45hello world 46EOT; 47 48// add arrays 49$index_array = array (1, 2, 3); 50$assoc_array = array ('one' => 1, 'two' => 2); 51 52//array of values to iterate over 53$inputs = array( 54 55 // int data 56 'int 0' => 0, 57 'int 1' => 1, 58 'int 12345' => 12345, 59 'int -12345' => -2345, 60 61 // float data 62 'float 10.5' => 10.5, 63 'float -10.5' => -10.5, 64 'float 12.3456789000e10' => 12.3456789000e10, 65 'float -12.3456789000e10' => -12.3456789000e10, 66 'float .5' => .5, 67 68 // array data 69 'empty array' => array(), 70 'int indexed array' => $index_array, 71 'associative array' => $assoc_array, 72 'nested arrays' => array('foo', $index_array, $assoc_array), 73 74 // null data 75 'uppercase NULL' => NULL, 76 'lowercase null' => null, 77 78 // boolean data 79 'lowercase true' => true, 80 'lowercase false' =>false, 81 'uppercase TRUE' =>TRUE, 82 'uppercase FALSE' =>FALSE, 83 84 // empty data 85 'empty string DQ' => "", 86 'empty string SQ' => '', 87 88 // object data 89 'instance of classWithToString' => new classWithToString(), 90 'instance of classWithoutToString' => new classWithoutToString(), 91 92 // undefined data 93 'undefined var' => @$undefined_var, 94 95 // unset data 96 'unset var' => @$unset_var, 97); 98 99// loop through each element of the array for filename 100 101foreach($inputs as $key =>$value) { 102 echo "\n--$key--\n"; 103 var_dump( file($value, $flags, $context) ); 104}; 105 106?> 107===DONE=== 108--EXPECTF-- 109*** Testing file() : usage variation *** 110 111--int 0-- 112Error: 2 - file(0): failed to open stream: No such file or directory, %s(%d) 113bool(false) 114 115--int 1-- 116Error: 2 - file(1): failed to open stream: No such file or directory, %s(%d) 117bool(false) 118 119--int 12345-- 120Error: 2 - file(12345): failed to open stream: No such file or directory, %s(%d) 121bool(false) 122 123--int -12345-- 124Error: 2 - file(-2345): failed to open stream: No such file or directory, %s(%d) 125bool(false) 126 127--float 10.5-- 128Error: 2 - file(10.5): failed to open stream: No such file or directory, %s(%d) 129bool(false) 130 131--float -10.5-- 132Error: 2 - file(-10.5): failed to open stream: No such file or directory, %s(%d) 133bool(false) 134 135--float 12.3456789000e10-- 136Error: 2 - file(123456789000): failed to open stream: No such file or directory, %s(%d) 137bool(false) 138 139--float -12.3456789000e10-- 140Error: 2 - file(-123456789000): failed to open stream: No such file or directory, %s(%d) 141bool(false) 142 143--float .5-- 144Error: 2 - file(0.5): failed to open stream: No such file or directory, %s(%d) 145bool(false) 146 147--empty array-- 148Error: 2 - file() expects parameter 1 to be string, array given, %s(%d) 149NULL 150 151--int indexed array-- 152Error: 2 - file() expects parameter 1 to be string, array given, %s(%d) 153NULL 154 155--associative array-- 156Error: 2 - file() expects parameter 1 to be string, array given, %s(%d) 157NULL 158 159--nested arrays-- 160Error: 2 - file() expects parameter 1 to be string, array given, %s(%d) 161NULL 162 163--uppercase NULL-- 164Error: 2 - file(): Filename cannot be empty, %s(%d) 165bool(false) 166 167--lowercase null-- 168Error: 2 - file(): Filename cannot be empty, %s(%d) 169bool(false) 170 171--lowercase true-- 172Error: 2 - file(1): failed to open stream: No such file or directory, %s(%d) 173bool(false) 174 175--lowercase false-- 176Error: 2 - file(): Filename cannot be empty, %s(%d) 177bool(false) 178 179--uppercase TRUE-- 180Error: 2 - file(1): failed to open stream: No such file or directory, %s(%d) 181bool(false) 182 183--uppercase FALSE-- 184Error: 2 - file(): Filename cannot be empty, %s(%d) 185bool(false) 186 187--empty string DQ-- 188Error: 2 - file(): Filename cannot be empty, %s(%d) 189bool(false) 190 191--empty string SQ-- 192Error: 2 - file(): Filename cannot be empty, %s(%d) 193bool(false) 194 195--instance of classWithToString-- 196Error: 2 - file(Class A object): failed to open stream: %s, %s(%d) 197bool(false) 198 199--instance of classWithoutToString-- 200Error: 2 - file() expects parameter 1 to be string, object given, %s(%d) 201NULL 202 203--undefined var-- 204Error: 2 - file(): Filename cannot be empty, %s(%d) 205bool(false) 206 207--unset var-- 208Error: 2 - file(): Filename cannot be empty, %s(%d) 209bool(false) 210===DONE=== 211