1--TEST-- 2Test fscanf() function: error conditions 3--FILE-- 4<?php 5/* 6 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] ); 7 Description: Parses input from a file according to a format 8*/ 9 10echo "*** Testing fscanf() for error conditions ***\n"; 11$file_path = dirname(__FILE__); 12 13$filename = "$file_path/fscanf_error.tmp"; 14$file_handle = fopen($filename, 'w'); 15if ($file_handle == false) 16 exit("Error:failed to open file $filename"); 17fwrite($file_handle, "hello world"); 18fclose($file_handle); 19 20// zero argument 21var_dump( fscanf() ); 22 23// single argument 24$file_handle = fopen($filename, 'r'); 25if ($file_handle == false) 26 exit("Error:failed to open file $filename"); 27var_dump( fscanf($file_handle) ); 28fclose($file_handle); 29 30// invalid file handle 31var_dump( fscanf($file_handle, "%s") ); 32 33// number of formats in format strings not matching the no of variables 34$file_handle = fopen($filename, 'r'); 35if ($file_handle == false) 36 exit("Error:failed to open file $filename"); 37var_dump( fscanf($file_handle, "%d%s%f", $int_var, $string_var) ); 38fclose($file_handle); 39 40// different invalid format strings 41$invalid_formats = array( $undefined_var, undefined_constant, 42 "%", "%h", "%.", "%d%m" 43 ); 44 45 46// looping to use various invalid formats with fscanf() 47foreach($invalid_formats as $format) { 48 $file_handle = fopen($filename, 'r'); 49 if ($file_handle == false) 50 exit("Error:failed to open file $filename"); 51 var_dump( fscanf($file_handle, $format) ); 52 fclose($file_handle); 53} 54 55echo "\n*** Done ***"; 56?> 57--CLEAN-- 58<?php 59$file_path = dirname(__FILE__); 60$filename = "$file_path/fscanf_error.tmp"; 61unlink($filename); 62?> 63--EXPECTF-- 64*** Testing fscanf() for error conditions *** 65 66Warning: fscanf() expects at least 2 parameters, 0 given in %s on line %d 67NULL 68 69Warning: fscanf() expects at least 2 parameters, 1 given in %s on line %d 70NULL 71 72Warning: fscanf(): supplied resource is not a valid File-Handle resource in %s on line %d 73bool(false) 74 75Warning: fscanf(): Different numbers of variable names and field specifiers in %s on line %d 76int(-1) 77 78Notice: Undefined variable: undefined_var in %s on line %d 79 80Warning: Use of undefined constant undefined_constant - assumed 'undefined_constant' (this will throw an Error in a future version of PHP) in %s on line %d 81array(0) { 82} 83array(0) { 84} 85 86Warning: fscanf(): Bad scan conversion character " in %s on line %d 87NULL 88 89Warning: fscanf(): Bad scan conversion character " in %s on line %d 90NULL 91 92Warning: fscanf(): Bad scan conversion character "." in %s on line %d 93NULL 94 95Warning: fscanf(): Bad scan conversion character "m" in %s on line %d 96NULL 97 98*** Done *** 99