1--TEST--
2Test sprintf() function : usage variations - typical format strings
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() : with  typical format strings ***\n";
11
12// initialising required variables
13$tempnum = 12345;
14$tempstring = "abcdefghjklmnpqrstuvwxyz";
15
16echo"\n-- Testing for '%%%.2f' as the format parameter --\n";
17var_dump(sprintf("%%%.2f", 1.23456789e10));
18
19echo"\n-- Testing for '%%' as the format parameter --\n";
20var_dump(sprintf("%%", 1.23456789e10));
21
22echo"\n-- Testing for precision value more than maximum --\n";
23var_dump(sprintf("%.988f", 1.23456789e10));
24
25echo"\n-- Testing for invalid width(-15) specifier --\n";
26var_dump(sprintf("%030.-15s", $tempstring));
27
28echo"\n-- Testing for '%X' as the format parameter --\n";
29var_dump(sprintf("%X", 12));
30
31echo"\n-- Testing for multiple format parameters --\n";
32var_dump(sprintf("%d  %s  %d\n", $tempnum, $tempstring, $tempnum));
33
34echo"\n-- Testing for excess of mixed type arguments  --\n";
35var_dump(sprintf("%s", $tempstring, $tempstring, $tempstring));
36
37echo "Done";
38?>
39--EXPECTF--
40*** Testing sprintf() : with  typical format strings ***
41
42-- Testing for '%%%.2f' as the format parameter --
43string(15) "%12345678900.00"
44
45-- Testing for '%%' as the format parameter --
46string(1) "%"
47
48-- Testing for precision value more than maximum --
49
50Notice: sprintf(): Requested precision of 988 digits was truncated to PHP maximum of %d digits in %s on line %d
51string(65) "12345678900.00000000000000000000000000000000000000000000000000000"
52
53-- Testing for invalid width(-15) specifier --
54string(3) "15s"
55
56-- Testing for '%X' as the format parameter --
57string(1) "C"
58
59-- Testing for multiple format parameters --
60string(39) "12345  abcdefghjklmnpqrstuvwxyz  12345
61"
62
63-- Testing for excess of mixed type arguments  --
64string(24) "abcdefghjklmnpqrstuvwxyz"
65Done
66