123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- --TEST--
- sprintf() formats with different types
- --FILE--
- <?php
- $formats = ['s', 'd', 'u', 'f', 'c', 'x'];
- $values = [null, false, true, 2, 3.5, "foo", [], [1], fopen(__FILE__, "r"), new stdClass];
- foreach ($formats as $format) {
- foreach ($values as $value) {
- echo "$format with " . (is_resource($value) ? "resource" : json_encode($value)) . ":\n";
- try {
- echo sprintf("%" . $format, $value), "\n";
- } catch (Error $e) {
- echo $e->getMessage(), "\n";
- }
- echo "\n";
- }
- }
- ?>
- --EXPECTF--
- %s with null:
- %s with false:
- %s with true:
- 1
- %s with 2:
- 2
- s with 3.5:
- 3.5
- %s with "foo":
- foo
- %s with []:
- Warning: Array to string conversion in %s on line %d
- Array
- %s with [1]:
- Warning: Array to string conversion in %s on line %d
- Array
- %s with resource:
- Resource id #%d
- %s with {}:
- Object of class stdClass could not be converted to string
- d with null:
- 0
- d with false:
- 0
- d with true:
- 1
- d with 2:
- 2
- d with 3.5:
- 3
- d with "foo":
- 0
- d with []:
- 0
- d with [1]:
- 1
- d with resource:
- %d
- d with {}:
- Warning: Object of class stdClass could not be converted to int in %s on line %d
- 1
- u with null:
- 0
- u with false:
- 0
- u with true:
- 1
- u with 2:
- 2
- u with 3.5:
- 3
- u with "foo":
- 0
- u with []:
- 0
- u with [1]:
- 1
- u with resource:
- %d
- u with {}:
- Warning: Object of class stdClass could not be converted to int in %s on line %d
- 1
- f with null:
- 0.000000
- f with false:
- 0.000000
- f with true:
- 1.000000
- f with 2:
- 2.000000
- f with 3.5:
- 3.500000
- f with "foo":
- 0.000000
- f with []:
- 0.000000
- f with [1]:
- 1.000000
- f with resource:
- %d.000000
- f with {}:
- Warning: Object of class stdClass could not be converted to float in %s on line %d
- 1.000000
- c with null:
- %0
- c with false:
- %0
- c with true:
- c with 2:
- c with 3.5:
- c with "foo":
- %0
- c with []:
- %0
- c with [1]:
- c with resource:
- %s
- c with {}:
- Warning: Object of class stdClass could not be converted to int in %s on line %d
- x with null:
- 0
- x with false:
- 0
- x with true:
- 1
- x with 2:
- 2
- x with 3.5:
- 3
- x with "foo":
- 0
- x with []:
- 0
- x with [1]:
- 1
- x with resource:
- %d
- x with {}:
- Warning: Object of class stdClass could not be converted to int in %s on line %d
- 1
|