count_variation2.phpt 2.9 KB

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