xref: /PHP-8.3/ext/standard/tests/strings/printf.phpt (revision ea256a21)
1--TEST--
2Test printf() function (32bit)
3--INI--
4precision=14
5--SKIPIF--
6<?php
7if (PHP_INT_MAX > 2147483647) {
8    die("skip 32bit test only");
9}
10?>
11--FILE--
12<?php
13
14/* Various input arrays for different format types */
15
16$float_variation  = array( "%f", "%-f", "%+f", "%7.2f", "%-7.2f", "%07.2f", "%-07.2f", "%'#7.2f" );
17$float_numbers    = array( 0, 1, -1, 0.32, -0.32, 3.4. -3.4, 2.54, -2.54, 1.2345678e99, -1.2345678e99 );
18
19$int_variation    = array( "%d", "%-d", "%+d", "%7.2d", "%-7.2d", "%07.2d", "%-07.2d", "%'#7.2d" );
20$int_numbers      = array( 0, 1, -1, 2.7, -2.7, 23333333, -23333333, "1234" );
21
22$char_variation   = array( 'a', "a", 67, -67, 99 );
23
24$string_variation = array( "%5s", "%-5s", "%05s", "%'#5s" );
25$strings          = array( NULL, "abc", 'aaa' );
26
27/* Checking warning messages */
28
29/* Zero argument */
30echo "\n*** Output for zero argument ***\n";
31try {
32    printf();
33} catch (TypeError $e) {
34    echo $e->getMessage(), "\n";
35}
36
37/* Number of arguments not matching as specified in format field */
38echo "\n*** Output for insufficient number of arguments ***\n";
39$string = "dingy%sflem%dwombat";
40$nbr = 5;
41$name = "voudras";
42try {
43    printf("%d $string %s", $nbr, $name);
44} catch (\ArgumentCountError $e) {
45    print('Error found: '.$e->getMessage());
46}
47
48
49/* Scalar argument */
50echo "\n*** Output for scalar argument ***\n";
51printf(3);
52
53/* Float type variations */
54
55$counter = 1;
56echo "\n\n*** Output for float type ***\n";
57echo "\n Input Float numbers variation array is:\n";
58print_r($float_numbers);
59
60foreach( $float_variation as $float_var )
61{
62 echo "\n\nFloat Iteration $counter";
63 foreach( $float_numbers as $float_num )
64 {
65  echo "\n";
66  printf( $float_var, $float_num );
67 }
68 $counter++;
69}
70
71
72/* Integer type variations */
73
74$counter = 1;
75echo "\n\n*** Output for integer type ***\n";
76echo "\n Input Integer numbers variation array is:\n";
77print_r($int_numbers);
78
79foreach( $int_variation as $int_var )
80{
81 echo "\n\nInteger Iteration $counter";
82 foreach( $int_numbers as $int_num )
83 {
84  echo "\n";
85  printf( $int_var, $int_num );
86 }
87 $counter++;
88}
89
90
91/* Binary type variations */
92
93echo "\n\n*** Output for binary type ***\n";
94echo "\n Input  numbers variation array is:\n";
95print_r($int_numbers);
96
97 foreach( $int_numbers as $bin_num )
98 {
99  echo "\n";
100  printf( "%b", $bin_num );
101 }
102
103
104/* Chararter type variations */
105echo "\n\n*** Output for char type ***\n";
106echo "\n Input Characters variation array is:\n";
107print_r($char_variation);
108
109foreach( $char_variation as $char )
110{
111 echo "\n";
112 printf( "%c", $char );
113}
114
115/* Scientific type variations */
116echo "\n\n*** Output for scientific type ***\n";
117echo "\n Input numbers variation array is:\n";
118print_r($int_numbers);
119
120foreach( $int_numbers as $num )
121{
122 echo "\n";
123 printf( "%e", $num );
124}
125
126/* Unsigned Integer type variation */
127echo "\n\n*** Output for unsigned integer type ***\n";
128echo "\n Input Integer numbers variation array is:\n";
129print_r($int_numbers);
130
131foreach( $int_numbers as $unsig_num )
132{
133 echo "\n";
134 printf( "%u", $unsig_num );
135}
136
137/* Octal type variations */
138echo "\n\n*** Output for octal type ***\n";
139echo "\n Input numbers variation array is:\n";
140print_r($int_numbers);
141
142foreach( $int_numbers as $octal_num )
143{
144 echo "\n";
145 printf( "%o", $octal_num );
146}
147
148/* Hexadecimal type variations */
149echo "\n\n*** Output for hexadecimal type ***\n";
150echo "\n Input numbers variation array is:\n";
151print_r($int_numbers);
152
153foreach( $int_numbers as $hexa_num )
154{
155 echo "\n";
156 printf( "%x", $hexa_num );
157}
158
159/* String type variations */
160echo "\n\n*** Output for string type ***\n";
161echo "\n Input Strings format variation array is:\n";
162print_r($string_variation);
163echo "\n Input strings variation array is:\n";
164print_r($strings);
165
166foreach( $string_variation as $string_var )
167{
168 foreach( $strings as $str )
169 {
170  echo "\n";
171  printf( $string_var, $str );
172 }
173}
174
175
176/* variations of %g type */
177$format_g = array("%g", "%.0g", "%+g", "%-g", "%-1.2g", "%+1.2g", "%G", "%.0G", "%+G", "%-G", "%-1.2G", "%+1.2G");
178
179echo "\n\n*** Output for '%g' type ***\n";
180echo "\n Input format variation array is:\n";
181print_r($format_g);
182
183foreach( $format_g as $formatg )
184{
185 printf("\n$formatg",123456);
186 printf("\n$formatg",-123456);
187}
188
189
190/* Some more typical cases */
191
192$tempnum = 12345;
193$tempstring = "abcdefghjklmnpqrstuvwxyz";
194
195echo"\n\n*** Output for '%%%.2f' as the format parameter ***\n";
196printf("%%%.2f",1.23456789e10);
197
198echo"\n\n*** Output for '%%' as the format parameter ***\n";
199printf("%%",1.23456789e10);
200
201echo"\n\n*** Output for precision value more than maximum ***\n";
202printf("%.988f",1.23456789e10);
203
204echo"\n\n*** Output for invalid width(-15) specifier ***\n";
205try {
206    printf("%030.-15s", $tempstring);
207} catch (ValueError $e) {
208    echo $e->getMessage();
209}
210
211echo"\n\n*** Output for '%F' as the format parameter ***\n";
212printf("%F",1.23456789e10);
213
214echo"\n\n*** Output for '%X' as the format parameter ***\n";
215printf("%X",12);
216
217echo"\n\n*** Output  with no format parameter ***\n";
218printf($tempnum);
219
220echo"\n\n*** Output for multiple format parameters ***\n";
221printf("%d  %s  %d\n", $tempnum, $tempstring, $tempnum);
222
223echo"\n\n*** Output for excess of mixed type arguments  ***\n";
224printf("%s", $tempstring, $tempstring, $tempstring);
225
226echo"\n\n*** Output for string format parameter and integer type argument ***\n";
227printf("%s", $tempnum);
228
229echo"\n\n*** Output for integer format parameter and string type argument ***\n";
230printf("%d", $tempstring);
231
232
233?>
234--EXPECTF--
235*** Output for zero argument ***
236printf() expects at least %d argument, %d given
237
238*** Output for insufficient number of arguments ***
239Error found: 5 arguments are required, 3 given
240*** Output for scalar argument ***
2413
242
243*** Output for float type ***
244
245 Input Float numbers variation array is:
246Array
247(
248    [0] => 0
249    [1] => 1
250    [2] => -1
251    [3] => 0.32
252    [4] => -0.32
253    [5] => 3.4-3.4
254    [6] => 2.54
255    [7] => -2.54
256    [8] => 1.2345678E+99
257    [9] => -1.2345678E+99
258)
259
260
261Float Iteration 1
2620.000000
2631.000000
264-1.000000
2650.320000
266-0.320000
2673.400000
2682.540000
269-2.540000
2701234567%d.000000
271-1234567%d.000000
272
273Float Iteration 2
2740.000000
2751.000000
276-1.000000
2770.320000
278-0.320000
2793.400000
2802.540000
281-2.540000
2821234567%d.000000
283-1234567%d.000000
284
285Float Iteration 3
286+0.000000
287+1.000000
288-1.000000
289+0.320000
290-0.320000
291+3.400000
292+2.540000
293-2.540000
294+1234567%d.000000
295-1234567%d.000000
296
297Float Iteration 4
298   0.00
299   1.00
300  -1.00
301   0.32
302  -0.32
303   3.40
304   2.54
305  -2.54
3061234567%d.00
307-1234567%d.00
308
309Float Iteration 5
3100.00
3111.00
312-1.00
3130.32
314-0.32
3153.40
3162.54
317-2.54
3181234567%d.00
319-1234567%d.00
320
321Float Iteration 6
3220000.00
3230001.00
324-001.00
3250000.32
326-000.32
3270003.40
3280002.54
329-002.54
3301234567%d.00
331-1234567%d.00
332
333Float Iteration 7
3340.00000
3351.00000
336-1.0000
3370.32000
338-0.3200
3393.40000
3402.54000
341-2.5400
3421234567%d.00
343-1234567%d.00
344
345Float Iteration 8
346###0.00
347###1.00
348##-1.00
349###0.32
350##-0.32
351###3.40
352###2.54
353##-2.54
3541234567%d.00
355-1234567%d.00
356
357*** Output for integer type ***
358
359 Input Integer numbers variation array is:
360Array
361(
362    [0] => 0
363    [1] => 1
364    [2] => -1
365    [3] => 2.7
366    [4] => -2.7
367    [5] => 23333333
368    [6] => -23333333
369    [7] => 1234
370)
371
372
373Integer Iteration 1
3740
3751
376-1
3772
378-2
37923333333
380-23333333
3811234
382
383Integer Iteration 2
3840
3851
386-1
3872
388-2
38923333333
390-23333333
3911234
392
393Integer Iteration 3
394+0
395+1
396-1
397+2
398-2
399+23333333
400-23333333
401+1234
402
403Integer Iteration 4
404      0
405      1
406     -1
407      2
408     -2
40923333333
410-23333333
411   1234
412
413Integer Iteration 5
4140
4151
416-1
4172
418-2
41923333333
420-23333333
4211234
422
423Integer Iteration 6
4240000000
4250000001
426-000001
4270000002
428-000002
42923333333
430-23333333
4310001234
432
433Integer Iteration 7
4340
4351
436-1
4372
438-2
43923333333
440-23333333
4411234
442
443Integer Iteration 8
444######0
445######1
446#####-1
447######2
448#####-2
44923333333
450-23333333
451###1234
452
453*** Output for binary type ***
454
455 Input  numbers variation array is:
456Array
457(
458    [0] => 0
459    [1] => 1
460    [2] => -1
461    [3] => 2.7
462    [4] => -2.7
463    [5] => 23333333
464    [6] => -23333333
465    [7] => 1234
466)
467
4680
4691
47011111111111111111111111111111111
47110
47211111111111111111111111111111110
4731011001000000100111010101
47411111110100110111111011000101011
47510011010010
476
477*** Output for char type ***
478
479 Input Characters variation array is:
480Array
481(
482    [0] => a
483    [1] => a
484    [2] => 67
485    [3] => -67
486    [4] => 99
487)
488
489%0
490%0
491C
492493c
494
495*** Output for scientific type ***
496
497 Input numbers variation array is:
498Array
499(
500    [0] => 0
501    [1] => 1
502    [2] => -1
503    [3] => 2.7
504    [4] => -2.7
505    [5] => 23333333
506    [6] => -23333333
507    [7] => 1234
508)
509
5100.000000e+0
5111.000000e+0
512-1.000000e+0
5132.700000e+0
514-2.700000e+0
5152.333333e+7
516-2.333333e+7
5171.234000e+3
518
519*** Output for unsigned integer type ***
520
521 Input Integer numbers variation array is:
522Array
523(
524    [0] => 0
525    [1] => 1
526    [2] => -1
527    [3] => 2.7
528    [4] => -2.7
529    [5] => 23333333
530    [6] => -23333333
531    [7] => 1234
532)
533
5340
5351
5364294967295
5372
5384294967294
53923333333
5404271633963
5411234
542
543*** Output for octal type ***
544
545 Input numbers variation array is:
546Array
547(
548    [0] => 0
549    [1] => 1
550    [2] => -1
551    [3] => 2.7
552    [4] => -2.7
553    [5] => 23333333
554    [6] => -23333333
555    [7] => 1234
556)
557
5580
5591
56037777777777
5612
56237777777776
563131004725
56437646773053
5652322
566
567*** Output for hexadecimal type ***
568
569 Input numbers variation array is:
570Array
571(
572    [0] => 0
573    [1] => 1
574    [2] => -1
575    [3] => 2.7
576    [4] => -2.7
577    [5] => 23333333
578    [6] => -23333333
579    [7] => 1234
580)
581
5820
5831
584ffffffff
5852
586fffffffe
58716409d5
588fe9bf62b
5894d2
590
591*** Output for string type ***
592
593 Input Strings format variation array is:
594Array
595(
596    [0] => %5s
597    [1] => %-5s
598    [2] => %r%%r05s
599    [3] => %'#5s
600)
601
602 Input strings variation array is:
603Array
604(
605    [0] =>
606    [1] => abc
607    [2] => aaa
608)
609
610
611  abc
612  aaa
613
614abc
615aaa
61600000
61700abc
61800aaa
619#####
620##abc
621##aaa
622
623*** Output for '%g' type ***
624
625 Input format variation array is:
626Array
627(
628    [0] => %g
629    [1] => %.0g
630    [2] => %+g
631    [3] => %-g
632    [4] => %-1.2g
633    [5] => %+1.2g
634    [6] => %G
635    [7] => %.0G
636    [8] => %+G
637    [9] => %-G
638    [10] => %-1.2G
639    [11] => %+1.2G
640)
641
642123456
643-123456
6441.0e+5
645-1.0e+5
646+123456
647-123456
648123456
649-123456
6501.2e+5
651-1.2e+5
652+1.2e+5
653-1.2e+5
654123456
655-123456
6561.0E+5
657-1.0E+5
658+123456
659-123456
660123456
661-123456
6621.2E+5
663-1.2E+5
664+1.2E+5
665-1.2E+5
666
667*** Output for '%%%.2f' as the format parameter ***
668%12345678900.00
669
670*** Output for '%%' as the format parameter ***
671%
672
673*** Output for precision value more than maximum ***
674
675Notice: printf(): Requested precision of 988 digits was truncated to PHP maximum of 53 digits in %s on line %d
67612345678900.00000000000000000000000000000000000000000000000000000
677
678*** Output for invalid width(-15) specifier ***
679Unknown format specifier "-"
680
681*** Output for '%F' as the format parameter ***
68212345678900.000000
683
684*** Output for '%X' as the format parameter ***
685C
686
687*** Output  with no format parameter ***
68812345
689
690*** Output for multiple format parameters ***
69112345  abcdefghjklmnpqrstuvwxyz  12345
692
693
694*** Output for excess of mixed type arguments  ***
695abcdefghjklmnpqrstuvwxyz
696
697*** Output for string format parameter and integer type argument ***
69812345
699
700*** Output for integer format parameter and string type argument ***
7010
702