1--TEST-- 2Test sscanf() function : usage variations - unexpected inputs for '$str' argument 3--FILE-- 4<?php 5/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] ) 6 * Description: Parses input from a string according to a format 7 * Source code: ext/standard/string.c 8*/ 9 10echo "*** Testing sscanf() function: with unexpected inputs for 'str' argument ***\n"; 11 12//get an unset variable 13$unset_var = 'string_val'; 14unset($unset_var); 15 16//defining a class 17class sample { 18 public function __toString() { 19 return "sample object"; 20 } 21} 22 23//getting the resource 24$file_handle = fopen(__FILE__, "r"); 25 26// array with different values for $input 27$inputs = array ( 28 29 // integer values 30/*1*/ 0, 31 1, 32 -2, 33 2147483647, 34 -2147483648, 35 36 // float values 37/*6*/ 10.5, 38 -20.5, 39 10.1234567e10, 40 41 // array values 42/*9*/ array(), 43 array(0), 44 array(1, 2), 45 46 // boolean values 47/*12*/ true, 48 false, 49 TRUE, 50 FALSE, 51 52 // null values 53/*16*/ NULL, 54 null, 55 56 // objects 57/*18*/ new sample(), 58 59 // resource 60/*19*/ $file_handle, 61 62 // undefined variable 63/*20*/ @$undefined_var, 64 65 // unset variable 66/*21*/ @$unset_var 67); 68 69//defining '$pad_length' argument 70$format = "%s"; 71 72// loop through with each element of the $inputs array to test sscanf() function 73$count = 1; 74foreach($inputs as $input) { 75 echo "-- Iteration $count --\n"; 76 var_dump( sscanf($input, $format) ); 77 $count ++; 78} 79 80fclose($file_handle); //closing the file handle 81 82?> 83===DONE=== 84--EXPECTF-- 85*** Testing sscanf() function: with unexpected inputs for 'str' argument *** 86-- Iteration 1 -- 87array(1) { 88 [0]=> 89 string(1) "0" 90} 91-- Iteration 2 -- 92array(1) { 93 [0]=> 94 string(1) "1" 95} 96-- Iteration 3 -- 97array(1) { 98 [0]=> 99 string(2) "-2" 100} 101-- Iteration 4 -- 102array(1) { 103 [0]=> 104 string(10) "2147483647" 105} 106-- Iteration 5 -- 107array(1) { 108 [0]=> 109 string(11) "-2147483648" 110} 111-- Iteration 6 -- 112array(1) { 113 [0]=> 114 string(4) "10.5" 115} 116-- Iteration 7 -- 117array(1) { 118 [0]=> 119 string(5) "-20.5" 120} 121-- Iteration 8 -- 122array(1) { 123 [0]=> 124 string(12) "101234567000" 125} 126-- Iteration 9 -- 127 128Warning: sscanf() expects parameter 1 to be string, array given in %s on line %d 129NULL 130-- Iteration 10 -- 131 132Warning: sscanf() expects parameter 1 to be string, array given in %s on line %d 133NULL 134-- Iteration 11 -- 135 136Warning: sscanf() expects parameter 1 to be string, array given in %s on line %d 137NULL 138-- Iteration 12 -- 139array(1) { 140 [0]=> 141 string(1) "1" 142} 143-- Iteration 13 -- 144NULL 145-- Iteration 14 -- 146array(1) { 147 [0]=> 148 string(1) "1" 149} 150-- Iteration 15 -- 151NULL 152-- Iteration 16 -- 153NULL 154-- Iteration 17 -- 155NULL 156-- Iteration 18 -- 157array(1) { 158 [0]=> 159 string(6) "sample" 160} 161-- Iteration 19 -- 162 163Warning: sscanf() expects parameter 1 to be string, resource given in %s on line %d 164NULL 165-- Iteration 20 -- 166NULL 167-- Iteration 21 -- 168NULL 169===DONE===