1--TEST-- 2Test file_get_contents() function : usage variation 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 = 'FileGetContentsVar5.tmp'; 26$absFile = dirname(__FILE__).'/'.$filename; 27$h = fopen($absFile,"w"); 28fwrite($h, "contents read"); 29fclose($h); 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 // int data 60 'int 0' => 0, 61 'int 1' => 1, 62 'int 12345' => 12345, 63 'int -12345' => -2345, 64 65 // float data 66 'float 10.5' => 10.5, 67 'float -10.5' => -10.5, 68 'float .5' => .5, 69 70 // array data 71 'empty array' => array(), 72 'int indexed array' => $index_array, 73 'associative array' => $assoc_array, 74 'nested arrays' => array('foo', $index_array, $assoc_array), 75 76 // null data 77 'uppercase NULL' => NULL, 78 'lowercase null' => null, 79 80 // boolean data 81 'lowercase true' => true, 82 'lowercase false' =>false, 83 'uppercase TRUE' =>TRUE, 84 'uppercase FALSE' =>FALSE, 85 86 // empty data 87 'empty string DQ' => "", 88 'empty string SQ' => '', 89 90 // string data 91 'string DQ' => "string", 92 'string SQ' => 'string', 93 'mixed case string' => "sTrInG", 94 'heredoc' => $heredoc, 95 96 // object data 97 'instance of classWithToString' => new classWithToString(), 98 'instance of classWithoutToString' => new classWithoutToString(), 99 100 // undefined data 101 'undefined var' => @$undefined_var, 102 103 // unset data 104 'unset var' => @$unset_var, 105); 106 107// loop through each element of the array for maxlen 108 109foreach($inputs as $key =>$value) { 110 echo "\n--$key--\n"; 111 var_dump( file_get_contents($absFile, false, null, 0, $value) ); 112}; 113 114unlink($absFile); 115 116?> 117===DONE=== 118--EXPECTF-- 119*** Testing file_get_contents() : usage variation *** 120 121--int 0-- 122string(%d) "" 123 124--int 1-- 125string(%d) "c" 126 127--int 12345-- 128string(%d) "contents read" 129 130--int -12345-- 131Error: 2 - file_get_contents(): length must be greater than or equal to zero, %s(%d) 132bool(false) 133 134--float 10.5-- 135string(%d) "contents r" 136 137--float -10.5-- 138Error: 2 - file_get_contents(): length must be greater than or equal to zero, %s(%d) 139bool(false) 140 141--float .5-- 142string(%d) "" 143 144--empty array-- 145Error: 2 - file_get_contents() expects parameter 5 to be long, array given, %s(%d) 146NULL 147 148--int indexed array-- 149Error: 2 - file_get_contents() expects parameter 5 to be long, array given, %s(%d) 150NULL 151 152--associative array-- 153Error: 2 - file_get_contents() expects parameter 5 to be long, array given, %s(%d) 154NULL 155 156--nested arrays-- 157Error: 2 - file_get_contents() expects parameter 5 to be long, array given, %s(%d) 158NULL 159 160--uppercase NULL-- 161string(%d) "" 162 163--lowercase null-- 164string(%d) "" 165 166--lowercase true-- 167string(%d) "c" 168 169--lowercase false-- 170string(%d) "" 171 172--uppercase TRUE-- 173string(%d) "c" 174 175--uppercase FALSE-- 176string(%d) "" 177 178--empty string DQ-- 179Error: 2 - file_get_contents() expects parameter 5 to be long, string given, %s(%d) 180NULL 181 182--empty string SQ-- 183Error: 2 - file_get_contents() expects parameter 5 to be long, string given, %s(%d) 184NULL 185 186--string DQ-- 187Error: 2 - file_get_contents() expects parameter 5 to be long, string given, %s(%d) 188NULL 189 190--string SQ-- 191Error: 2 - file_get_contents() expects parameter 5 to be long, string given, %s(%d) 192NULL 193 194--mixed case string-- 195Error: 2 - file_get_contents() expects parameter 5 to be long, string given, %s(%d) 196NULL 197 198--heredoc-- 199Error: 2 - file_get_contents() expects parameter 5 to be long, string given, %s(%d) 200NULL 201 202--instance of classWithToString-- 203Error: 2 - file_get_contents() expects parameter 5 to be long, object given, %s(%d) 204NULL 205 206--instance of classWithoutToString-- 207Error: 2 - file_get_contents() expects parameter 5 to be long, object given, %s(%d) 208NULL 209 210--undefined var-- 211string(%d) "" 212 213--unset var-- 214string(%d) "" 215===DONE=== 216