1--TEST-- 2Test printf() function : usage variations - with all types of values for arg1 argument 3--FILE-- 4<?php 5/* Prototype : int printf ( string $format [, mixed $args [, mixed $... ]] ) 6 * Description: Produces output according to format . 7 * Source code: ext/standard/formatted_print.c 8 */ 9 10error_reporting(E_ALL & ~E_NOTICE); 11 12echo "*** Testing printf() : 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/*1*/ 0, 38 1, 39 12345, 40 -2345, 41 42 // float data 43/*5*/ 10.5, 44 -10.5, 45 10.1234567e10, 46 10.7654321E-10, 47 .5, 48 49 // array data 50/*10*/ array(), 51 array(0), 52 array(1), 53 array(1, 2), 54 array('color' => 'red', 'item' => 'pen'), 55 56 // null data 57/*15*/ NULL, 58 null, 59 60 // boolean data 61/*17*/ true, 62 false, 63 TRUE, 64 FALSE, 65 66 // empty data 67/*21*/ "", 68 '', 69 70 // string data 71/*23*/ "string", 72 'string', 73 74 // object data 75/*25*/ new sample(), 76 77 // undefined data 78/*26*/ @$undefined_var, 79 80 // unset data 81/*27*/ @$unset_var, 82 83 // resource data 84/*28*/ $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 $result = printf($format, $value); 95 echo "\n"; 96 var_dump($result); 97 98 // with three arguments 99 $result = printf($format, $value, $arg2); 100 echo "\n"; 101 var_dump($result); 102 103 $count++; 104}; 105 106// closing the resource 107fclose($file_handle); 108 109?> 110===DONE=== 111--EXPECTF-- 112*** Testing printf() : with different types of values passed for arg1 argument *** 113 114-- Iteration 1 -- 1150 116int(1) 1170 118int(1) 119 120-- Iteration 2 -- 1211 122int(1) 1231 124int(1) 125 126-- Iteration 3 -- 12712345 128int(5) 12912345 130int(5) 131 132-- Iteration 4 -- 133-2345 134int(5) 135-2345 136int(5) 137 138-- Iteration 5 -- 13910.5 140int(4) 14110.5 142int(4) 143 144-- Iteration 6 -- 145-10.5 146int(5) 147-10.5 148int(5) 149 150-- Iteration 7 -- 151101234567000 152int(12) 153101234567000 154int(12) 155 156-- Iteration 8 -- 1571.07654321E-9 158int(13) 1591.07654321E-9 160int(13) 161 162-- Iteration 9 -- 1630.5 164int(3) 1650.5 166int(3) 167 168-- Iteration 10 -- 169Array 170int(5) 171Array 172int(5) 173 174-- Iteration 11 -- 175Array 176int(5) 177Array 178int(5) 179 180-- Iteration 12 -- 181Array 182int(5) 183Array 184int(5) 185 186-- Iteration 13 -- 187Array 188int(5) 189Array 190int(5) 191 192-- Iteration 14 -- 193Array 194int(5) 195Array 196int(5) 197 198-- Iteration 15 -- 199 200int(0) 201 202int(0) 203 204-- Iteration 16 -- 205 206int(0) 207 208int(0) 209 210-- Iteration 17 -- 2111 212int(1) 2131 214int(1) 215 216-- Iteration 18 -- 217 218int(0) 219 220int(0) 221 222-- Iteration 19 -- 2231 224int(1) 2251 226int(1) 227 228-- Iteration 20 -- 229 230int(0) 231 232int(0) 233 234-- Iteration 21 -- 235 236int(0) 237 238int(0) 239 240-- Iteration 22 -- 241 242int(0) 243 244int(0) 245 246-- Iteration 23 -- 247string 248int(6) 249string 250int(6) 251 252-- Iteration 24 -- 253string 254int(6) 255string 256int(6) 257 258-- Iteration 25 -- 259Object 260int(6) 261Object 262int(6) 263 264-- Iteration 26 -- 265 266int(0) 267 268int(0) 269 270-- Iteration 27 -- 271 272int(0) 273 274int(0) 275 276-- Iteration 28 -- 277Resource id #%d 278int(%d) 279Resource id #%d 280int(%d) 281===DONE=== 282