sprintf_variation54.phpt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. --TEST--
  2. sprintf() formats with different types
  3. --FILE--
  4. <?php
  5. $formats = ['s', 'd', 'u', 'f', 'c', 'x'];
  6. $values = [null, false, true, 2, 3.5, "foo", [], [1], fopen(__FILE__, "r"), new stdClass];
  7. foreach ($formats as $format) {
  8. foreach ($values as $value) {
  9. echo "$format with " . (is_resource($value) ? "resource" : json_encode($value)) . ":\n";
  10. try {
  11. echo sprintf("%" . $format, $value), "\n";
  12. } catch (Error $e) {
  13. echo $e->getMessage(), "\n";
  14. }
  15. echo "\n";
  16. }
  17. }
  18. ?>
  19. --EXPECTF--
  20. %s with null:
  21. %s with false:
  22. %s with true:
  23. 1
  24. %s with 2:
  25. 2
  26. s with 3.5:
  27. 3.5
  28. %s with "foo":
  29. foo
  30. %s with []:
  31. Warning: Array to string conversion in %s on line %d
  32. Array
  33. %s with [1]:
  34. Warning: Array to string conversion in %s on line %d
  35. Array
  36. %s with resource:
  37. Resource id #%d
  38. %s with {}:
  39. Object of class stdClass could not be converted to string
  40. d with null:
  41. 0
  42. d with false:
  43. 0
  44. d with true:
  45. 1
  46. d with 2:
  47. 2
  48. d with 3.5:
  49. 3
  50. d with "foo":
  51. 0
  52. d with []:
  53. 0
  54. d with [1]:
  55. 1
  56. d with resource:
  57. %d
  58. d with {}:
  59. Warning: Object of class stdClass could not be converted to int in %s on line %d
  60. 1
  61. u with null:
  62. 0
  63. u with false:
  64. 0
  65. u with true:
  66. 1
  67. u with 2:
  68. 2
  69. u with 3.5:
  70. 3
  71. u with "foo":
  72. 0
  73. u with []:
  74. 0
  75. u with [1]:
  76. 1
  77. u with resource:
  78. %d
  79. u with {}:
  80. Warning: Object of class stdClass could not be converted to int in %s on line %d
  81. 1
  82. f with null:
  83. 0.000000
  84. f with false:
  85. 0.000000
  86. f with true:
  87. 1.000000
  88. f with 2:
  89. 2.000000
  90. f with 3.5:
  91. 3.500000
  92. f with "foo":
  93. 0.000000
  94. f with []:
  95. 0.000000
  96. f with [1]:
  97. 1.000000
  98. f with resource:
  99. %d.000000
  100. f with {}:
  101. Warning: Object of class stdClass could not be converted to float in %s on line %d
  102. 1.000000
  103. c with null:
  104. %0
  105. c with false:
  106. %0
  107. c with true:
  108. 
  109. c with 2:
  110. 
  111. c with 3.5:
  112. 
  113. c with "foo":
  114. %0
  115. c with []:
  116. %0
  117. c with [1]:
  118. 
  119. c with resource:
  120. %s
  121. c with {}:
  122. Warning: Object of class stdClass could not be converted to int in %s on line %d
  123. 
  124. x with null:
  125. 0
  126. x with false:
  127. 0
  128. x with true:
  129. 1
  130. x with 2:
  131. 2
  132. x with 3.5:
  133. 3
  134. x with "foo":
  135. 0
  136. x with []:
  137. 0
  138. x with [1]:
  139. 1
  140. x with resource:
  141. %d
  142. x with {}:
  143. Warning: Object of class stdClass could not be converted to int in %s on line %d
  144. 1