xref: /PHP-5.6/ext/standard/tests/strings/printf.phpt (revision 610c7fbe)
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";
31printf();
32
33/* Number of arguments not matching as specified in format field */
34echo "\n*** Output for insufficient number of arguments ***\n";
35$string = "dingy%sflem%dwombat";
36$nbr = 5;
37$name = "voudras";
38printf("%d $string %s", $nbr, $name);
39
40
41/* Scalar argument */
42echo "\n*** Output for scalar argument ***\n";
43printf(3);
44
45/* NULL argument */
46echo "\n*** Output for NULL as argument ***\n";
47printf(NULL);
48
49
50/* Float type variations */
51
52$counter = 1;
53echo "\n\n*** Output for float type ***\n";
54echo "\n Input Float numbers variation array is:\n";
55print_r($float_numbers);
56
57foreach( $float_variation as $float_var )
58{
59 echo "\n\nFloat Iteration $counter";
60 foreach( $float_numbers as $float_num )
61 {
62  echo "\n";
63  printf( $float_var, $float_num );
64 }
65 $counter++;
66}
67
68
69/* Integer type variations */
70
71$counter = 1;
72echo "\n\n*** Output for integer type ***\n";
73echo "\n Input Integer numbers variation array is:\n";
74print_r($int_numbers);
75
76foreach( $int_variation as $int_var )
77{
78 echo "\n\nInteger Iteration $counter";
79 foreach( $int_numbers as $int_num )
80 {
81  echo "\n";
82  printf( $int_var, $int_num );
83 }
84 $counter++;
85}
86
87
88/* Binary type variations */
89
90echo "\n\n*** Output for binary type ***\n";
91echo "\n Input  numbers variation array is:\n";
92print_r($int_numbers);
93
94 foreach( $int_numbers as $bin_num )
95 {
96  echo "\n";
97  printf( "%b", $bin_num );
98 }
99
100
101/* Chararter type variations */
102echo "\n\n*** Output for char type ***\n";
103echo "\n Input Characters variation array is:\n";
104print_r($char_variation);
105
106foreach( $char_variation as $char )
107{
108 echo "\n";
109 printf( "%c", $char );
110}
111
112/* Scientific type variations */
113echo "\n\n*** Output for scientific type ***\n";
114echo "\n Input numbers variation array is:\n";
115print_r($int_numbers);
116
117foreach( $int_numbers as $num )
118{
119 echo "\n";
120 printf( "%e", $num );
121}
122
123/* Unsigned Integer type variation */
124echo "\n\n*** Output for unsigned integer type ***\n";
125echo "\n Input Integer numbers variation array is:\n";
126print_r($int_numbers);
127
128foreach( $int_numbers as $unsig_num )
129{
130 echo "\n";
131 printf( "%u", $unsig_num );
132}
133
134/* Octal type variations */
135echo "\n\n*** Output for octal type ***\n";
136echo "\n Input numbers variation array is:\n";
137print_r($int_numbers);
138
139foreach( $int_numbers as $octal_num )
140{
141 echo "\n";
142 printf( "%o", $octal_num );
143}
144
145/* Hexadecimal type variations */
146echo "\n\n*** Output for hexadecimal type ***\n";
147echo "\n Input numbers variation array is:\n";
148print_r($int_numbers);
149
150foreach( $int_numbers as $hexa_num )
151{
152 echo "\n";
153 printf( "%x", $hexa_num );
154}
155
156/* String type variations */
157echo "\n\n*** Output for string type ***\n";
158echo "\n Input Strings format variation array is:\n";
159print_r($string_variation);
160echo "\n Input strings variation array is:\n";
161print_r($strings);
162
163foreach( $string_variation as $string_var )
164{
165 foreach( $strings as $str )
166 {
167  echo "\n";
168  printf( $string_var, $str );
169 }
170}
171
172
173/* variations of %g type */
174$format_g = array("%g", "%.0g", "%+g", "%-g", "%-1.2g", "%+1.2g", "%G", "%.0G", "%+G", "%-G", "%-1.2G", "%+1.2G");
175
176echo "\n\n*** Output for '%g' type ***\n";
177echo "\n Input format variation array is:\n";
178print_r($format_g);
179
180foreach( $format_g as $formatg )
181{
182 printf("\n$formatg",123456);
183 printf("\n$formatg",-123456);
184}
185
186
187/* Some more typical cases */
188
189$tempnum = 12345;
190$tempstring = "abcdefghjklmnpqrstuvwxyz";
191
192echo"\n\n*** Output for '%%%.2f' as the format parameter ***\n";
193printf("%%%.2f",1.23456789e10);
194
195echo"\n\n*** Output for '%%' as the format parameter ***\n";
196printf("%%",1.23456789e10);
197
198echo"\n\n*** Output for precision value more than maximum ***\n";
199printf("%.988f",1.23456789e10);
200
201echo"\n\n*** Output for invalid width(-15) specifier ***\n";
202printf("%030.-15s", $tempstring);
203
204echo"\n\n*** Output for '%F' as the format parameter ***\n";
205printf("%F",1.23456789e10);
206
207echo"\n\n*** Output for '%X' as the format parameter ***\n";
208printf("%X",12);
209
210echo"\n\n*** Output  with no format parameter ***\n";
211printf($tempnum);
212
213echo"\n\n*** Output for multiple format parameters ***\n";
214printf("%d  %s  %d\n", $tempnum, $tempstring, $tempnum);
215
216echo"\n\n*** Output for excess of mixed type arguments  ***\n";
217printf("%s", $tempstring, $tempstring, $tempstring);
218
219echo"\n\n*** Output for string format parameter and integer type argument ***\n";
220printf("%s", $tempnum);
221
222echo"\n\n*** Output for integer format parameter and string type argument ***\n";
223printf("%d", $tempstring);
224
225
226?>
227--EXPECTF--
228*** Output for zero argument ***
229
230Warning: printf() expects at least %d parameter, %d given in %s on line %d
231
232*** Output for insufficient number of arguments ***
233
234Warning: printf(): Too few arguments in %s on line %d
235
236*** Output for scalar argument ***
2373
238*** Output for NULL as argument ***
239
240
241*** Output for float type ***
242
243 Input Float numbers variation array is:
244Array
245(
246    [0] => 0
247    [1] => 1
248    [2] => -1
249    [3] => 0.32
250    [4] => -0.32
251    [5] => 3.4-3.4
252    [6] => 2.54
253    [7] => -2.54
254    [8] => 1.2345678E+99
255    [9] => -1.2345678E+99
256)
257
258
259Float Iteration 1
2600.000000
2611.000000
262-1.000000
2630.320000
264-0.320000
2653.400000
2662.540000
267-2.540000
2681234567%d.000000
269-1234567%d.000000
270
271Float Iteration 2
2720.000000
2731.000000
274-1.000000
2750.320000
276-0.320000
2773.400000
2782.540000
279-2.540000
2801234567%d.000000
281-1234567%d.000000
282
283Float Iteration 3
284+0.000000
285+1.000000
286-1.000000
287+0.320000
288-0.320000
289+3.400000
290+2.540000
291-2.540000
292+1234567%d.000000
293-1234567%d.000000
294
295Float Iteration 4
296   0.00
297   1.00
298  -1.00
299   0.32
300  -0.32
301   3.40
302   2.54
303  -2.54
3041234567%d.00
305-1234567%d.00
306
307Float Iteration 5
3080.00
3091.00
310-1.00
3110.32
312-0.32
3133.40
3142.54
315-2.54
3161234567%d.00
317-1234567%d.00
318
319Float Iteration 6
3200000.00
3210001.00
322-001.00
3230000.32
324-000.32
3250003.40
3260002.54
327-002.54
3281234567%d.00
329-1234567%d.00
330
331Float Iteration 7
3320.00000
3331.00000
334-1.0000
3350.32000
336-0.3200
3373.40000
3382.54000
339-2.5400
3401234567%d.00
341-1234567%d.00
342
343Float Iteration 8
344###0.00
345###1.00
346##-1.00
347###0.32
348##-0.32
349###3.40
350###2.54
351##-2.54
3521234567%d.00
353-1234567%d.00
354
355*** Output for integer type ***
356
357 Input Integer numbers variation array is:
358Array
359(
360    [0] => 0
361    [1] => 1
362    [2] => -1
363    [3] => 2.7
364    [4] => -2.7
365    [5] => 23333333
366    [6] => -23333333
367    [7] => 1234
368)
369
370
371Integer Iteration 1
3720
3731
374-1
3752
376-2
37723333333
378-23333333
3791234
380
381Integer Iteration 2
3820
3831
384-1
3852
386-2
38723333333
388-23333333
3891234
390
391Integer Iteration 3
392+0
393+1
394-1
395+2
396-2
397+23333333
398-23333333
399+1234
400
401Integer Iteration 4
402      0
403      1
404     -1
405      2
406     -2
40723333333
408-23333333
409   1234
410
411Integer Iteration 5
4120
4131
414-1
4152
416-2
41723333333
418-23333333
4191234
420
421Integer Iteration 6
4220000000
4230000001
424-000001
4250000002
426-000002
42723333333
428-23333333
4290001234
430
431Integer Iteration 7
4320
4331
434-1
4352
436-2
43723333333
438-23333333
4391234
440
441Integer Iteration 8
442######0
443######1
444#####-1
445######2
446#####-2
44723333333
448-23333333
449###1234
450
451*** Output for binary type ***
452
453 Input  numbers variation array is:
454Array
455(
456    [0] => 0
457    [1] => 1
458    [2] => -1
459    [3] => 2.7
460    [4] => -2.7
461    [5] => 23333333
462    [6] => -23333333
463    [7] => 1234
464)
465
4660
4671
46811111111111111111111111111111111
46910
47011111111111111111111111111111110
4711011001000000100111010101
47211111110100110111111011000101011
47310011010010
474
475*** Output for char type ***
476
477 Input Characters variation array is:
478Array
479(
480    [0] => a
481    [1] => a
482    [2] => 67
483    [3] => -67
484    [4] => 99
485)
486
487488489C
490491c
492
493*** Output for scientific type ***
494
495 Input numbers variation array is:
496Array
497(
498    [0] => 0
499    [1] => 1
500    [2] => -1
501    [3] => 2.7
502    [4] => -2.7
503    [5] => 23333333
504    [6] => -23333333
505    [7] => 1234
506)
507
5080.000000e+0
5091.000000e+0
510-1.000000e+0
5112.700000e+0
512-2.700000e+0
5132.333333e+7
514-2.333333e+7
5151.234000e+3
516
517*** Output for unsigned integer type ***
518
519 Input Integer numbers variation array is:
520Array
521(
522    [0] => 0
523    [1] => 1
524    [2] => -1
525    [3] => 2.7
526    [4] => -2.7
527    [5] => 23333333
528    [6] => -23333333
529    [7] => 1234
530)
531
5320
5331
5344294967295
5352
5364294967294
53723333333
5384271633963
5391234
540
541*** Output for octal type ***
542
543 Input numbers variation array is:
544Array
545(
546    [0] => 0
547    [1] => 1
548    [2] => -1
549    [3] => 2.7
550    [4] => -2.7
551    [5] => 23333333
552    [6] => -23333333
553    [7] => 1234
554)
555
5560
5571
55837777777777
5592
56037777777776
561131004725
56237646773053
5632322
564
565*** Output for hexadecimal type ***
566
567 Input numbers variation array is:
568Array
569(
570    [0] => 0
571    [1] => 1
572    [2] => -1
573    [3] => 2.7
574    [4] => -2.7
575    [5] => 23333333
576    [6] => -23333333
577    [7] => 1234
578)
579
5800
5811
582ffffffff
5832
584fffffffe
58516409d5
586fe9bf62b
5874d2
588
589*** Output for string type ***
590
591 Input Strings format variation array is:
592Array
593(
594    [0] => %5s
595    [1] => %-5s
596    [2] => %05s
597    [3] => %'#5s
598)
599
600 Input strings variation array is:
601Array
602(
603    [0] =>
604    [1] => abc
605    [2] => aaa
606)
607
608
609  abc
610  aaa
611
612abc
613aaa
61400000
61500abc
61600aaa
617#####
618##abc
619##aaa
620
621*** Output for '%g' type ***
622
623 Input format variation array is:
624Array
625(
626    [0] => %g
627    [1] => %.0g
628    [2] => %+g
629    [3] => %-g
630    [4] => %-1.2g
631    [5] => %+1.2g
632    [6] => %G
633    [7] => %.0G
634    [8] => %+G
635    [9] => %-G
636    [10] => %-1.2G
637    [11] => %+1.2G
638)
639
640123456
641-123456
6421.0e+5
643-1.0e+5
644+123456
645-123456
646123456
647-123456
6481.2e+5
649-1.2e+5
650+1.2e+5
651-1.2e+5
652123456
653-123456
6541.0E+5
655-1.0E+5
656+123456
657-123456
658123456
659-123456
6601.2E+5
661-1.2E+5
662+1.2E+5
663-1.2E+5
664
665*** Output for '%%%.2f' as the format parameter ***
666%12345678900.00
667
668*** Output for '%%' as the format parameter ***
669%
670
671*** Output for precision value more than maximum ***
672
673Notice: printf(): Requested precision of 988 digits was truncated to PHP maximum of 53 digits in %s on line %d
67412345678900.00000000000000000000000000000000000000000000000000000
675
676*** Output for invalid width(-15) specifier ***
67715s
678
679*** Output for '%F' as the format parameter ***
68012345678900.000000
681
682*** Output for '%X' as the format parameter ***
683C
684
685*** Output  with no format parameter ***
68612345
687
688*** Output for multiple format parameters ***
68912345  abcdefghjklmnpqrstuvwxyz  12345
690
691
692*** Output for excess of mixed type arguments  ***
693abcdefghjklmnpqrstuvwxyz
694
695*** Output for string format parameter and integer type argument ***
69612345
697
698*** Output for integer format parameter and string type argument ***
6990
700