1--TEST-- 2Test sprintf() function : usage variations - int formats with string values 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 10echo "*** Testing sprintf() : integer formats with string values ***\n"; 11 12// array of strings used to test the function 13$string_values = array ( 14 "", 15 '', 16 "0", 17 '0', 18 "1", 19 '1', 20 "-123abc", 21 "123abc", 22 "+123abc", 23 "\x01", 24 '\x01', 25 "\01", 26 '\01', 27 'string', 28 "string", 29 "true", 30 "FALSE", 31 'false', 32 'TRUE', 33 "NULL", 34 'null' 35); 36 37// various integer formats 38$int_formats = array( 39 "%d", "%hd", "%ld", 40 "%Ld", " %d", "%d ", 41 "\t%d", "\n%d", "%4d", 42 "%30d", "%[0-9]", "%*d" 43); 44 45$count = 1; 46foreach($string_values as $string_value) { 47 echo "\n-- Iteration $count --\n"; 48 49 foreach($int_formats as $format) { 50 var_dump( sprintf($format, $string_value) ); 51 } 52 $count++; 53}; 54 55echo "Done"; 56?> 57--EXPECTF-- 58*** Testing sprintf() : integer formats with string values *** 59 60-- Iteration 1 -- 61string(1) "0" 62string(1) "d" 63string(1) "0" 64string(1) "d" 65string(2) " 0" 66string(2) "0 " 67string(2) " 0" 68string(2) " 690" 70string(4) " 0" 71string(30) " 0" 72string(4) "0-9]" 73string(1) "d" 74 75-- Iteration 2 -- 76string(1) "0" 77string(1) "d" 78string(1) "0" 79string(1) "d" 80string(2) " 0" 81string(2) "0 " 82string(2) " 0" 83string(2) " 840" 85string(4) " 0" 86string(30) " 0" 87string(4) "0-9]" 88string(1) "d" 89 90-- Iteration 3 -- 91string(1) "0" 92string(1) "d" 93string(1) "0" 94string(1) "d" 95string(2) " 0" 96string(2) "0 " 97string(2) " 0" 98string(2) " 990" 100string(4) " 0" 101string(30) " 0" 102string(4) "0-9]" 103string(1) "d" 104 105-- Iteration 4 -- 106string(1) "0" 107string(1) "d" 108string(1) "0" 109string(1) "d" 110string(2) " 0" 111string(2) "0 " 112string(2) " 0" 113string(2) " 1140" 115string(4) " 0" 116string(30) " 0" 117string(4) "0-9]" 118string(1) "d" 119 120-- Iteration 5 -- 121string(1) "1" 122string(1) "d" 123string(1) "1" 124string(1) "d" 125string(2) " 1" 126string(2) "1 " 127string(2) " 1" 128string(2) " 1291" 130string(4) " 1" 131string(30) " 1" 132string(4) "0-9]" 133string(1) "d" 134 135-- Iteration 6 -- 136string(1) "1" 137string(1) "d" 138string(1) "1" 139string(1) "d" 140string(2) " 1" 141string(2) "1 " 142string(2) " 1" 143string(2) " 1441" 145string(4) " 1" 146string(30) " 1" 147string(4) "0-9]" 148string(1) "d" 149 150-- Iteration 7 -- 151string(4) "-123" 152string(1) "d" 153string(4) "-123" 154string(1) "d" 155string(5) " -123" 156string(5) "-123 " 157string(5) " -123" 158string(5) " 159-123" 160string(4) "-123" 161string(30) " -123" 162string(4) "0-9]" 163string(1) "d" 164 165-- Iteration 8 -- 166string(3) "123" 167string(1) "d" 168string(3) "123" 169string(1) "d" 170string(4) " 123" 171string(4) "123 " 172string(4) " 123" 173string(4) " 174123" 175string(4) " 123" 176string(30) " 123" 177string(4) "0-9]" 178string(1) "d" 179 180-- Iteration 9 -- 181string(3) "123" 182string(1) "d" 183string(3) "123" 184string(1) "d" 185string(4) " 123" 186string(4) "123 " 187string(4) " 123" 188string(4) " 189123" 190string(4) " 123" 191string(30) " 123" 192string(4) "0-9]" 193string(1) "d" 194 195-- Iteration 10 -- 196string(1) "0" 197string(1) "d" 198string(1) "0" 199string(1) "d" 200string(2) " 0" 201string(2) "0 " 202string(2) " 0" 203string(2) " 2040" 205string(4) " 0" 206string(30) " 0" 207string(4) "0-9]" 208string(1) "d" 209 210-- Iteration 11 -- 211string(1) "0" 212string(1) "d" 213string(1) "0" 214string(1) "d" 215string(2) " 0" 216string(2) "0 " 217string(2) " 0" 218string(2) " 2190" 220string(4) " 0" 221string(30) " 0" 222string(4) "0-9]" 223string(1) "d" 224 225-- Iteration 12 -- 226string(1) "0" 227string(1) "d" 228string(1) "0" 229string(1) "d" 230string(2) " 0" 231string(2) "0 " 232string(2) " 0" 233string(2) " 2340" 235string(4) " 0" 236string(30) " 0" 237string(4) "0-9]" 238string(1) "d" 239 240-- Iteration 13 -- 241string(1) "0" 242string(1) "d" 243string(1) "0" 244string(1) "d" 245string(2) " 0" 246string(2) "0 " 247string(2) " 0" 248string(2) " 2490" 250string(4) " 0" 251string(30) " 0" 252string(4) "0-9]" 253string(1) "d" 254 255-- Iteration 14 -- 256string(1) "0" 257string(1) "d" 258string(1) "0" 259string(1) "d" 260string(2) " 0" 261string(2) "0 " 262string(2) " 0" 263string(2) " 2640" 265string(4) " 0" 266string(30) " 0" 267string(4) "0-9]" 268string(1) "d" 269 270-- Iteration 15 -- 271string(1) "0" 272string(1) "d" 273string(1) "0" 274string(1) "d" 275string(2) " 0" 276string(2) "0 " 277string(2) " 0" 278string(2) " 2790" 280string(4) " 0" 281string(30) " 0" 282string(4) "0-9]" 283string(1) "d" 284 285-- Iteration 16 -- 286string(1) "0" 287string(1) "d" 288string(1) "0" 289string(1) "d" 290string(2) " 0" 291string(2) "0 " 292string(2) " 0" 293string(2) " 2940" 295string(4) " 0" 296string(30) " 0" 297string(4) "0-9]" 298string(1) "d" 299 300-- Iteration 17 -- 301string(1) "0" 302string(1) "d" 303string(1) "0" 304string(1) "d" 305string(2) " 0" 306string(2) "0 " 307string(2) " 0" 308string(2) " 3090" 310string(4) " 0" 311string(30) " 0" 312string(4) "0-9]" 313string(1) "d" 314 315-- Iteration 18 -- 316string(1) "0" 317string(1) "d" 318string(1) "0" 319string(1) "d" 320string(2) " 0" 321string(2) "0 " 322string(2) " 0" 323string(2) " 3240" 325string(4) " 0" 326string(30) " 0" 327string(4) "0-9]" 328string(1) "d" 329 330-- Iteration 19 -- 331string(1) "0" 332string(1) "d" 333string(1) "0" 334string(1) "d" 335string(2) " 0" 336string(2) "0 " 337string(2) " 0" 338string(2) " 3390" 340string(4) " 0" 341string(30) " 0" 342string(4) "0-9]" 343string(1) "d" 344 345-- Iteration 20 -- 346string(1) "0" 347string(1) "d" 348string(1) "0" 349string(1) "d" 350string(2) " 0" 351string(2) "0 " 352string(2) " 0" 353string(2) " 3540" 355string(4) " 0" 356string(30) " 0" 357string(4) "0-9]" 358string(1) "d" 359 360-- Iteration 21 -- 361string(1) "0" 362string(1) "d" 363string(1) "0" 364string(1) "d" 365string(2) " 0" 366string(2) "0 " 367string(2) " 0" 368string(2) " 3690" 370string(4) " 0" 371string(30) " 0" 372string(4) "0-9]" 373string(1) "d" 374Done 375