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