xref: /PHP-8.3/tests/strings/002.phpt (revision 41df5c06)
1--TEST--
2Formatted print functions
3--FILE--
4<?php
5
6$fp = fopen("php://stdout", "w") or die("Bug!!");
7$x = fprintf($fp, "fprintf test 1:%.5s", "abcdefghij");
8echo "\n";
9var_dump($x);
10
11printf("printf test 1:%s\n", "simple string");
12printf("printf test 2:%d\n", 42);
13printf("printf test 3:%f\n", 10.0/3);
14printf("printf test 4:%.10f\n", 10.0/3);
15printf("printf test 5:%-10.2f\n", 2.5);
16printf("printf test 6:%-010.2f\n", 2.5);
17printf("printf test 7:%010.2f\n", 2.5);
18printf("printf test 8:<%20s>\n", "foo");
19printf("printf test 9:<%-20s>\n", "bar");
20printf("printf test 10: 123456789012345\n");
21printf("printf test 10:<%15s>\n", "hoyesterettsjustitiarius");
22printf("printf test 11: 123456789012345678901234567890\n");
23printf("printf test 11:<%30s>\n", "hoyesterettsjustitiarius");
24printf("printf test 12:%5.2f\n", -12.34);
25printf("printf test 13:%5d\n", -12);
26printf("printf test 14:%c\n", 64);
27printf("printf test 15:%b\n", 170);
28printf("printf test 16:%x\n", 170);
29printf("printf test 17:%X\n", 170);
30printf("printf test 18:%16b\n", 170);
31printf("printf test 19:%16x\n", 170);
32printf("printf test 20:%16X\n", 170);
33printf("printf test 21:%016b\n", 170);
34printf("printf test 22:%016x\n", 170);
35printf("printf test 23:%016X\n", 170);
36printf("printf test 24:%.5s\n", "abcdefghij");
37printf("printf test 25:%-2s\n", "gazonk");
38printf("printf test 26:%2\$d %1\$d\n", 1, 2);
39printf("printf test 27:%3\$d %d %d\n", 1, 2, 3);
40printf("printf test 28:%2\$02d %1\$2d\n", 1, 2);
41printf("printf test 29:%2\$-2d %1\$2d\n", 1, 2);
42try {
43    print("printf test 30:"); printf("%0\$s", 1); print("x\n");
44} catch(\ValueError $e) {
45    print('Error found: '.$e->getMessage()."\n");
46}
47vprintf("vprintf test 1:%2\$-2d %1\$2d\n", array(1, 2));
48
49
50?>
51--EXPECT--
52fprintf test 1:abcde
53int(20)
54printf test 1:simple string
55printf test 2:42
56printf test 3:3.333333
57printf test 4:3.3333333333
58printf test 5:2.50
59printf test 6:2.50000000
60printf test 7:0000002.50
61printf test 8:<                 foo>
62printf test 9:<bar                 >
63printf test 10: 123456789012345
64printf test 10:<hoyesterettsjustitiarius>
65printf test 11: 123456789012345678901234567890
66printf test 11:<      hoyesterettsjustitiarius>
67printf test 12:-12.34
68printf test 13:  -12
69printf test 14:@
70printf test 15:10101010
71printf test 16:aa
72printf test 17:AA
73printf test 18:        10101010
74printf test 19:              aa
75printf test 20:              AA
76printf test 21:0000000010101010
77printf test 22:00000000000000aa
78printf test 23:00000000000000AA
79printf test 24:abcde
80printf test 25:gazonk
81printf test 26:2 1
82printf test 27:3 1 2
83printf test 28:02  1
84printf test 29:2   1
85printf test 30:Error found: Argument number specifier must be greater than zero and less than 2147483647
86vprintf test 1:2   1
87