printf_variation2.phpt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. --TEST--
  2. Test printf() function : usage variations - with all types of values for arg1 argument
  3. --FILE--
  4. <?php
  5. /* Prototype : int printf ( string $format [, mixed $args [, mixed $... ]] )
  6. * Description: Produces output according to format .
  7. * Source code: ext/standard/formatted_print.c
  8. */
  9. error_reporting(E_ALL & ~E_NOTICE);
  10. echo "*** Testing printf() : with different types of values passed for arg1 argument ***\n";
  11. // initialing required variables
  12. $format = '%s';
  13. $arg2 = 'third argument';
  14. //get an unset variable
  15. $unset_var = 10;
  16. unset ($unset_var);
  17. // declaring class
  18. class sample
  19. {
  20. public function __toString() {
  21. return "Object";
  22. }
  23. }
  24. // creating a file resource
  25. $file_handle = fopen(__FILE__, 'r');
  26. //array of values to iterate over
  27. $values = array(
  28. // int data
  29. /*1*/ 0,
  30. 1,
  31. 12345,
  32. -2345,
  33. // float data
  34. /*5*/ 10.5,
  35. -10.5,
  36. 10.1234567e10,
  37. 10.7654321E-10,
  38. .5,
  39. // array data
  40. /*10*/ array(),
  41. array(0),
  42. array(1),
  43. array(1, 2),
  44. array('color' => 'red', 'item' => 'pen'),
  45. // null data
  46. /*15*/ NULL,
  47. null,
  48. // boolean data
  49. /*17*/ true,
  50. false,
  51. TRUE,
  52. FALSE,
  53. // empty data
  54. /*21*/ "",
  55. '',
  56. // string data
  57. /*23*/ "string",
  58. 'string',
  59. // object data
  60. /*25*/ new sample(),
  61. // undefined data
  62. /*26*/ @$undefined_var,
  63. // unset data
  64. /*27*/ @$unset_var,
  65. // resource data
  66. /*28*/ $file_handle
  67. );
  68. // loop through each element of the array for arg1
  69. $count = 1;
  70. foreach($values as $value) {
  71. echo "\n-- Iteration $count --\n";
  72. // with two arguments
  73. $result = printf($format, $value);
  74. echo "\n";
  75. var_dump($result);
  76. // with three arguments
  77. $result = printf($format, $value, $arg2);
  78. echo "\n";
  79. var_dump($result);
  80. $count++;
  81. };
  82. // closing the resource
  83. fclose($file_handle);
  84. ?>
  85. ===DONE===
  86. --EXPECTF--
  87. *** Testing printf() : with different types of values passed for arg1 argument ***
  88. -- Iteration 1 --
  89. 0
  90. int(1)
  91. 0
  92. int(1)
  93. -- Iteration 2 --
  94. 1
  95. int(1)
  96. 1
  97. int(1)
  98. -- Iteration 3 --
  99. 12345
  100. int(5)
  101. 12345
  102. int(5)
  103. -- Iteration 4 --
  104. -2345
  105. int(5)
  106. -2345
  107. int(5)
  108. -- Iteration 5 --
  109. 10.5
  110. int(4)
  111. 10.5
  112. int(4)
  113. -- Iteration 6 --
  114. -10.5
  115. int(5)
  116. -10.5
  117. int(5)
  118. -- Iteration 7 --
  119. 101234567000
  120. int(12)
  121. 101234567000
  122. int(12)
  123. -- Iteration 8 --
  124. 1.07654321E-9
  125. int(13)
  126. 1.07654321E-9
  127. int(13)
  128. -- Iteration 9 --
  129. 0.5
  130. int(3)
  131. 0.5
  132. int(3)
  133. -- Iteration 10 --
  134. Array
  135. int(5)
  136. Array
  137. int(5)
  138. -- Iteration 11 --
  139. Array
  140. int(5)
  141. Array
  142. int(5)
  143. -- Iteration 12 --
  144. Array
  145. int(5)
  146. Array
  147. int(5)
  148. -- Iteration 13 --
  149. Array
  150. int(5)
  151. Array
  152. int(5)
  153. -- Iteration 14 --
  154. Array
  155. int(5)
  156. Array
  157. int(5)
  158. -- Iteration 15 --
  159. int(0)
  160. int(0)
  161. -- Iteration 16 --
  162. int(0)
  163. int(0)
  164. -- Iteration 17 --
  165. 1
  166. int(1)
  167. 1
  168. int(1)
  169. -- Iteration 18 --
  170. int(0)
  171. int(0)
  172. -- Iteration 19 --
  173. 1
  174. int(1)
  175. 1
  176. int(1)
  177. -- Iteration 20 --
  178. int(0)
  179. int(0)
  180. -- Iteration 21 --
  181. int(0)
  182. int(0)
  183. -- Iteration 22 --
  184. int(0)
  185. int(0)
  186. -- Iteration 23 --
  187. string
  188. int(6)
  189. string
  190. int(6)
  191. -- Iteration 24 --
  192. string
  193. int(6)
  194. string
  195. int(6)
  196. -- Iteration 25 --
  197. Object
  198. int(6)
  199. Object
  200. int(6)
  201. -- Iteration 26 --
  202. int(0)
  203. int(0)
  204. -- Iteration 27 --
  205. int(0)
  206. int(0)
  207. -- Iteration 28 --
  208. Resource id #%d
  209. int(%d)
  210. Resource id #%d
  211. int(%d)
  212. ===DONE===