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