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