1--TEST-- 2Test file_get_contents() function : usage variation - different types for context. 3--CREDITS-- 4Dave Kelsey <d_kelsey@uk.ibm.com> 5--FILE-- 6<?php 7/* Prototype : string file_get_contents(string filename [, bool use_include_path [, resource context [, long offset [, long maxlen]]]]) 8 * Description: Read the entire file into a string 9 * Source code: ext/standard/file.c 10 * Alias to functions: 11 */ 12 13echo "*** Testing file_get_contents() : usage variation ***\n"; 14 15// Define error handler 16function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { 17 if (error_reporting() != 0) { 18 // report non-silenced errors 19 echo "Error: $err_no - $err_msg, $filename($linenum)\n"; 20 } 21} 22set_error_handler('test_error_handler'); 23 24// Initialise function arguments not being substituted (if any) 25$filename = 'FileGetContentsVar4.tmp'; 26$absFile = dirname(__FILE__).'/'.$filename; 27$h = fopen($absFile,"w"); 28fwrite($h, "contents read"); 29fclose($h); 30 31$fileRes = fopen(__FILE__,'r'); 32$strContext = stream_context_create(); 33 34//get an unset variable 35$unset_var = 10; 36unset ($unset_var); 37 38// define some classes 39class classWithToString 40{ 41 public function __toString() { 42 return "Class A object"; 43 } 44} 45 46class classWithoutToString 47{ 48} 49 50// heredoc string 51$heredoc = <<<EOT 52hello world 53EOT; 54 55// add arrays 56$index_array = array (1, 2, 3); 57$assoc_array = array ('one' => 1, 'two' => 2); 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 //non context resource 112 'file resource' => $fileRes, 113 114 //valid stream context 115 'stream context' => $strContext, 116); 117 118// loop through each element of the array for context 119 120foreach($inputs as $key =>$value) { 121 echo "\n--$key--\n"; 122 var_dump( file_get_contents($absFile, false, $value) ); 123}; 124 125unlink($absFile); 126fclose($fileRes); 127 128?> 129===DONE=== 130--EXPECTF-- 131*** Testing file_get_contents() : usage variation *** 132 133--int 0-- 134Error: 2 - file_get_contents() expects parameter 3 to be resource, integer given, %s(%d) 135NULL 136 137--int 1-- 138Error: 2 - file_get_contents() expects parameter 3 to be resource, integer given, %s(%d) 139NULL 140 141--int 12345-- 142Error: 2 - file_get_contents() expects parameter 3 to be resource, integer given, %s(%d) 143NULL 144 145--int -12345-- 146Error: 2 - file_get_contents() expects parameter 3 to be resource, integer given, %s(%d) 147NULL 148 149--float 10.5-- 150Error: 2 - file_get_contents() expects parameter 3 to be resource, double given, %s(%d) 151NULL 152 153--float -10.5-- 154Error: 2 - file_get_contents() expects parameter 3 to be resource, double given, %s(%d) 155NULL 156 157--float 12.3456789000e10-- 158Error: 2 - file_get_contents() expects parameter 3 to be resource, double given, %s(%d) 159NULL 160 161--float -12.3456789000e10-- 162Error: 2 - file_get_contents() expects parameter 3 to be resource, double given, %s(%d) 163NULL 164 165--float .5-- 166Error: 2 - file_get_contents() expects parameter 3 to be resource, double given, %s(%d) 167NULL 168 169--empty array-- 170Error: 2 - file_get_contents() expects parameter 3 to be resource, array given, %s(%d) 171NULL 172 173--int indexed array-- 174Error: 2 - file_get_contents() expects parameter 3 to be resource, array given, %s(%d) 175NULL 176 177--associative array-- 178Error: 2 - file_get_contents() expects parameter 3 to be resource, array given, %s(%d) 179NULL 180 181--nested arrays-- 182Error: 2 - file_get_contents() expects parameter 3 to be resource, array given, %s(%d) 183NULL 184 185--uppercase NULL-- 186string(%d) "contents read" 187 188--lowercase null-- 189string(%d) "contents read" 190 191--lowercase true-- 192Error: 2 - file_get_contents() expects parameter 3 to be resource, boolean given, %s(%d) 193NULL 194 195--lowercase false-- 196Error: 2 - file_get_contents() expects parameter 3 to be resource, boolean given, %s(%d) 197NULL 198 199--uppercase TRUE-- 200Error: 2 - file_get_contents() expects parameter 3 to be resource, boolean given, %s(%d) 201NULL 202 203--uppercase FALSE-- 204Error: 2 - file_get_contents() expects parameter 3 to be resource, boolean given, %s(%d) 205NULL 206 207--empty string DQ-- 208Error: 2 - file_get_contents() expects parameter 3 to be resource, string given, %s(%d) 209NULL 210 211--empty string SQ-- 212Error: 2 - file_get_contents() expects parameter 3 to be resource, string given, %s(%d) 213NULL 214 215--string DQ-- 216Error: 2 - file_get_contents() expects parameter 3 to be resource, string given, %s(%d) 217NULL 218 219--string SQ-- 220Error: 2 - file_get_contents() expects parameter 3 to be resource, string given, %s(%d) 221NULL 222 223--mixed case string-- 224Error: 2 - file_get_contents() expects parameter 3 to be resource, string given, %s(%d) 225NULL 226 227--heredoc-- 228Error: 2 - file_get_contents() expects parameter 3 to be resource, string given, %s(%d) 229NULL 230 231--instance of classWithToString-- 232Error: 2 - file_get_contents() expects parameter 3 to be resource, object given, %s(%d) 233NULL 234 235--instance of classWithoutToString-- 236Error: 2 - file_get_contents() expects parameter 3 to be resource, object given, %s(%d) 237NULL 238 239--undefined var-- 240string(%d) "contents read" 241 242--unset var-- 243string(%d) "contents read" 244 245--file resource-- 246Error: 2 - file_get_contents(): supplied resource is not a valid Stream-Context resource, %s(%d) 247string(%d) "contents read" 248 249--stream context-- 250string(%d) "contents read" 251===DONE===