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