1--TEST-- 2Test sprintf() function : usage variations - unexpected values for format argument 3--FILE-- 4<?php 5/* Prototype : string sprintf(string $format [, mixed $arg1 [, mixed ...]]) 6 * Description: Return a formatted string 7 * Source code: ext/standard/formatted_print.c 8*/ 9 10/* 11* Testing sprintf() : with different unexpected values for format argument other than the strings 12*/ 13 14echo "*** Testing sprintf() : with unexpected values for format argument ***\n"; 15 16// initialing required variables 17$arg1 = "second arg"; 18$arg2 = "third arg"; 19 20//get an unset variable 21$unset_var = 10; 22unset ($unset_var); 23 24// declaring class 25class sample 26{ 27 public function __toString() { 28 return "Object"; 29 } 30} 31 32// creating a file resource 33$file_handle = fopen(__FILE__, 'r'); 34 35//array of values to iterate over 36$values = array( 37 38 // int data 39 0, 40 1, 41 12345, 42 -2345, 43 44 // float data 45 10.5, 46 -10.5, 47 10.1234567e10, 48 10.7654321E-10, 49 .5, 50 51 // array data 52 array(), 53 array(0), 54 array(1), 55 array(1, 2), 56 array('color' => 'red', 'item' => 'pen'), 57 58 // null data 59 NULL, 60 null, 61 62 // boolean data 63 true, 64 false, 65 TRUE, 66 FALSE, 67 68 // empty data 69 "", 70 '', 71 72 // object data 73 new sample(), 74 75 // undefined data 76 @$undefined_var, 77 78 // unset data 79 @$unset_var, 80 81 // resource data 82 $file_handle 83); 84 85// loop through each element of the array for format 86 87$count = 1; 88foreach($values as $value) { 89 echo "\n-- Iteration $count --\n"; 90 91 // with default argument 92 var_dump( sprintf($value) ); 93 94 // with two arguments 95 var_dump( sprintf($value, $arg1) ); 96 97 // with three arguments 98 var_dump( sprintf($value, $arg1, $arg2) ); 99 100 $count++; 101}; 102 103// close the resource 104fclose($file_handle); 105 106echo "Done"; 107?> 108--EXPECTF-- 109*** Testing sprintf() : with unexpected values for format argument *** 110 111-- Iteration 1 -- 112string(1) "0" 113string(1) "0" 114string(1) "0" 115 116-- Iteration 2 -- 117string(1) "1" 118string(1) "1" 119string(1) "1" 120 121-- Iteration 3 -- 122string(5) "12345" 123string(5) "12345" 124string(5) "12345" 125 126-- Iteration 4 -- 127string(5) "-2345" 128string(5) "-2345" 129string(5) "-2345" 130 131-- Iteration 5 -- 132string(4) "10.5" 133string(4) "10.5" 134string(4) "10.5" 135 136-- Iteration 6 -- 137string(5) "-10.5" 138string(5) "-10.5" 139string(5) "-10.5" 140 141-- Iteration 7 -- 142string(12) "101234567000" 143string(12) "101234567000" 144string(12) "101234567000" 145 146-- Iteration 8 -- 147string(13) "1.07654321E-9" 148string(13) "1.07654321E-9" 149string(13) "1.07654321E-9" 150 151-- Iteration 9 -- 152string(3) "0.5" 153string(3) "0.5" 154string(3) "0.5" 155 156-- Iteration 10 -- 157 158Notice: Array to string conversion in %s on line %d 159string(5) "Array" 160 161Notice: Array to string conversion in %s on line %d 162string(5) "Array" 163 164Notice: Array to string conversion in %s on line %d 165string(5) "Array" 166 167-- Iteration 11 -- 168 169Notice: Array to string conversion in %s on line %d 170string(5) "Array" 171 172Notice: Array to string conversion in %s on line %d 173string(5) "Array" 174 175Notice: Array to string conversion in %s on line %d 176string(5) "Array" 177 178-- Iteration 12 -- 179 180Notice: Array to string conversion in %s on line %d 181string(5) "Array" 182 183Notice: Array to string conversion in %s on line %d 184string(5) "Array" 185 186Notice: Array to string conversion in %s on line %d 187string(5) "Array" 188 189-- Iteration 13 -- 190 191Notice: Array to string conversion in %s on line %d 192string(5) "Array" 193 194Notice: Array to string conversion in %s on line %d 195string(5) "Array" 196 197Notice: Array to string conversion in %s on line %d 198string(5) "Array" 199 200-- Iteration 14 -- 201 202Notice: Array to string conversion in %s on line %d 203string(5) "Array" 204 205Notice: Array to string conversion in %s on line %d 206string(5) "Array" 207 208Notice: Array to string conversion in %s on line %d 209string(5) "Array" 210 211-- Iteration 15 -- 212string(0) "" 213string(0) "" 214string(0) "" 215 216-- Iteration 16 -- 217string(0) "" 218string(0) "" 219string(0) "" 220 221-- Iteration 17 -- 222string(1) "1" 223string(1) "1" 224string(1) "1" 225 226-- Iteration 18 -- 227string(0) "" 228string(0) "" 229string(0) "" 230 231-- Iteration 19 -- 232string(1) "1" 233string(1) "1" 234string(1) "1" 235 236-- Iteration 20 -- 237string(0) "" 238string(0) "" 239string(0) "" 240 241-- Iteration 21 -- 242string(0) "" 243string(0) "" 244string(0) "" 245 246-- Iteration 22 -- 247string(0) "" 248string(0) "" 249string(0) "" 250 251-- Iteration 23 -- 252string(6) "Object" 253string(6) "Object" 254string(6) "Object" 255 256-- Iteration 24 -- 257string(0) "" 258string(0) "" 259string(0) "" 260 261-- Iteration 25 -- 262string(0) "" 263string(0) "" 264string(0) "" 265 266-- Iteration 26 -- 267string(%d) "Resource id #%d" 268string(%d) "Resource id #%d" 269string(%d) "Resource id #%d" 270Done