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