1--TEST-- 2Test fscanf() function: usage variations - integer formats with boolean 3--FILE-- 4<?php 5 6/* 7 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] ); 8 Description: Parses input from a file according to a format 9*/ 10 11/* Test fscanf() to scan boolean data using different integer format types */ 12 13$file_path = dirname(__FILE__); 14 15echo "*** Test fscanf(): different integer format types with boolean data ***\n"; 16 17// create a file 18$filename = "$file_path/fscanf_variation7.tmp"; 19$file_handle = fopen($filename, "w"); 20if($file_handle == false) 21 exit("Error:failed to open file $filename"); 22 23// array of boolean types 24$bool_types = array ( 25 true, 26 false, 27 TRUE, 28 FALSE, 29); 30 31$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d"); 32 33$counter = 1; 34 35// writing to the file 36foreach($bool_types as $value) { 37 @fprintf($file_handle, $value); 38 @fprintf($file_handle, "\n"); 39} 40// closing the file 41fclose($file_handle); 42 43// opening the file for reading 44$file_handle = fopen($filename, "r"); 45if($file_handle == false) { 46 exit("Error:failed to open file $filename"); 47} 48 49$counter = 1; 50// reading the values from file using different integer formats 51foreach($int_formats as $int_format) { 52 // rewind the file so that for every foreach iteration the file pointer starts from bof 53 rewind($file_handle); 54 echo "\n-- iteration $counter --\n"; 55 while( !feof($file_handle) ) { 56 var_dump( fscanf($file_handle,$int_format) ); 57 } 58 $counter++; 59} 60 61echo "\n*** Done ***"; 62?> 63--CLEAN-- 64<?php 65$file_path = dirname(__FILE__); 66$filename = "$file_path/fscanf_variation7.tmp"; 67unlink($filename); 68?> 69--EXPECTF-- 70*** Test fscanf(): different integer format types with boolean data *** 71 72-- iteration 1 -- 73array(1) { 74 [0]=> 75 int(1) 76} 77NULL 78array(1) { 79 [0]=> 80 int(1) 81} 82NULL 83bool(false) 84 85-- iteration 2 -- 86array(1) { 87 [0]=> 88 int(1) 89} 90NULL 91array(1) { 92 [0]=> 93 int(1) 94} 95NULL 96bool(false) 97 98-- iteration 3 -- 99array(1) { 100 [0]=> 101 int(1) 102} 103NULL 104array(1) { 105 [0]=> 106 int(1) 107} 108NULL 109bool(false) 110 111-- iteration 4 -- 112array(1) { 113 [0]=> 114 int(1) 115} 116NULL 117array(1) { 118 [0]=> 119 int(1) 120} 121NULL 122bool(false) 123 124-- iteration 5 -- 125array(1) { 126 [0]=> 127 int(1) 128} 129NULL 130array(1) { 131 [0]=> 132 int(1) 133} 134NULL 135bool(false) 136 137-- iteration 6 -- 138array(1) { 139 [0]=> 140 int(1) 141} 142NULL 143array(1) { 144 [0]=> 145 int(1) 146} 147NULL 148bool(false) 149 150-- iteration 7 -- 151 152Warning: fscanf(): Bad scan conversion character " " in %s on line %d 153NULL 154 155Warning: fscanf(): Bad scan conversion character " " in %s on line %d 156NULL 157 158Warning: fscanf(): Bad scan conversion character " " in %s on line %d 159NULL 160 161Warning: fscanf(): Bad scan conversion character " " in %s on line %d 162NULL 163bool(false) 164 165-- iteration 8 -- 166array(1) { 167 [0]=> 168 int(1) 169} 170NULL 171array(1) { 172 [0]=> 173 int(1) 174} 175NULL 176bool(false) 177 178-- iteration 9 -- 179array(1) { 180 [0]=> 181 int(1) 182} 183NULL 184array(1) { 185 [0]=> 186 int(1) 187} 188NULL 189bool(false) 190 191-- iteration 10 -- 192array(1) { 193 [0]=> 194 int(1) 195} 196NULL 197array(1) { 198 [0]=> 199 int(1) 200} 201NULL 202bool(false) 203 204-- iteration 11 -- 205array(1) { 206 [0]=> 207 int(1) 208} 209NULL 210array(1) { 211 [0]=> 212 int(1) 213} 214NULL 215bool(false) 216 217-- iteration 12 -- 218array(1) { 219 [0]=> 220 string(1) "1" 221} 222array(1) { 223 [0]=> 224 NULL 225} 226array(1) { 227 [0]=> 228 string(1) "1" 229} 230array(1) { 231 [0]=> 232 NULL 233} 234bool(false) 235 236-- iteration 13 -- 237array(0) { 238} 239NULL 240array(0) { 241} 242NULL 243bool(false) 244 245*** Done *** 246 247