count_variation2.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. --TEST--
  2. Test count() function : usage variations - Pass different data types as $mode arg
  3. --FILE--
  4. <?php
  5. /* Prototype : int count(mixed $var [, int $mode])
  6. * Description: Count the number of elements in a variable (usually an array)
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Pass different data types as $mode argument to count() to test behaviour
  11. */
  12. echo "*** Testing count() : usage variations ***\n";
  13. // Initialise function arguments not being substituted
  14. $var = array(1, 2, array ('one', 'two'));
  15. //get an unset variable
  16. $unset_var = 10;
  17. unset ($unset_var);
  18. // get a class
  19. class classA
  20. {
  21. public function __toString() {
  22. return "Class A object";
  23. }
  24. }
  25. // heredoc string
  26. $heredoc = <<<EOT
  27. hello world
  28. EOT;
  29. // get a resource variable
  30. $fp = fopen(__FILE__, "r");
  31. // unexpected values to be passed to $mode argument
  32. $inputs = array(
  33. // int data
  34. /*1*/ 0,
  35. 1,
  36. 12345,
  37. -2345,
  38. // float data
  39. /*5*/ 10.5,
  40. -10.5,
  41. 12.3456789000e10,
  42. 12.3456789000E-10,
  43. .5,
  44. // null data
  45. /*10*/ NULL,
  46. null,
  47. // boolean data
  48. /*12*/ true,
  49. false,
  50. TRUE,
  51. FALSE,
  52. // empty data
  53. /*16*/ "",
  54. '',
  55. // string data
  56. /*18*/ "string",
  57. 'string',
  58. $heredoc,
  59. // object data
  60. /*21*/ new classA(),
  61. // undefined data
  62. /*22*/ @$undefined_var,
  63. // unset data
  64. /*23*/ @$unset_var,
  65. // resource variable
  66. /*24*/ $fp
  67. );
  68. // loop through each element of $inputs to check the behavior of count()
  69. $iterator = 1;
  70. foreach($inputs as $input) {
  71. echo "\n-- Iteration $iterator --\n";
  72. var_dump( count($var, $input) );
  73. $iterator++;
  74. };
  75. fclose($fp);
  76. echo "Done";
  77. ?>
  78. --EXPECTF--
  79. *** Testing count() : usage variations ***
  80. -- Iteration 1 --
  81. int(3)
  82. -- Iteration 2 --
  83. int(5)
  84. -- Iteration 3 --
  85. int(3)
  86. -- Iteration 4 --
  87. int(3)
  88. -- Iteration 5 --
  89. int(3)
  90. -- Iteration 6 --
  91. int(3)
  92. -- Iteration 7 --
  93. int(3)
  94. -- Iteration 8 --
  95. int(3)
  96. -- Iteration 9 --
  97. int(3)
  98. -- Iteration 10 --
  99. int(3)
  100. -- Iteration 11 --
  101. int(3)
  102. -- Iteration 12 --
  103. int(5)
  104. -- Iteration 13 --
  105. int(3)
  106. -- Iteration 14 --
  107. int(5)
  108. -- Iteration 15 --
  109. int(3)
  110. -- Iteration 16 --
  111. Warning: count() expects parameter 2 to be long, string given in %s on line %d
  112. NULL
  113. -- Iteration 17 --
  114. Warning: count() expects parameter 2 to be long, string given in %s on line %d
  115. NULL
  116. -- Iteration 18 --
  117. Warning: count() expects parameter 2 to be long, string given in %s on line %d
  118. NULL
  119. -- Iteration 19 --
  120. Warning: count() expects parameter 2 to be long, string given in %s on line %d
  121. NULL
  122. -- Iteration 20 --
  123. Warning: count() expects parameter 2 to be long, string given in %s on line %d
  124. NULL
  125. -- Iteration 21 --
  126. Warning: count() expects parameter 2 to be long, object given in %s on line %d
  127. NULL
  128. -- Iteration 22 --
  129. int(3)
  130. -- Iteration 23 --
  131. int(3)
  132. -- Iteration 24 --
  133. Warning: count() expects parameter 2 to be long, resource given in %s on line %d
  134. NULL
  135. Done