count_variation1.phpt 2.2 KB

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