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