1--TEST-- 2Test 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/* 11 * aPass different data types as $var argument to count() to test behaviour 12 */ 13 14echo "*** Testing count() : usage variations ***\n"; 15 16//get an unset variable 17$unset_var = 10; 18unset ($unset_var); 19 20// get a class 21class classA 22{ 23 public function __toString() { 24 return "Class A object"; 25 } 26} 27 28// heredoc string 29$heredoc = <<<EOT 30hello world 31EOT; 32 33// get a resource variable 34$fp = fopen(__FILE__, "r"); 35 36// unexpected values to be passed to $var argument 37$inputs = array( 38 39 // int data 40/*1*/ 0, 41 1, 42 12345, 43 -2345, 44 45 // float data 46/*5*/ 10.5, 47 -10.5, 48 12.3456789000e10, 49 12.3456789000E-10, 50 .5, 51 52 // null data 53/*10*/ NULL, 54 null, 55 56 // boolean data 57/*12*/ true, 58 false, 59 TRUE, 60 FALSE, 61 62 // empty data 63/*16*/ "", 64 '', 65 66 // string data 67/*18*/ "string", 68 'string', 69 $heredoc, 70 71 // object data 72/*21*/ new classA(), 73 74 // undefined data 75/*22*/ @$undefined_var, 76 77 // unset data 78/*23*/ @$unset_var, 79 80 // resource variable 81/*24*/ $fp 82); 83 84// loop through each element of $inputs to check the behavior of count() 85$iterator = 1; 86foreach($inputs as $input) { 87 echo "\n-- Iteration $iterator --\n"; 88 var_dump( count($input) ); 89 $iterator++; 90}; 91 92fclose($fp); 93 94echo "Done"; 95?> 96--EXPECTF-- 97*** Testing count() : usage variations *** 98 99-- Iteration 1 -- 100 101Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 102int(1) 103 104-- Iteration 2 -- 105 106Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 107int(1) 108 109-- Iteration 3 -- 110 111Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 112int(1) 113 114-- Iteration 4 -- 115 116Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 117int(1) 118 119-- Iteration 5 -- 120 121Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 122int(1) 123 124-- Iteration 6 -- 125 126Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 127int(1) 128 129-- Iteration 7 -- 130 131Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 132int(1) 133 134-- Iteration 8 -- 135 136Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 137int(1) 138 139-- Iteration 9 -- 140 141Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 142int(1) 143 144-- Iteration 10 -- 145 146Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 147int(0) 148 149-- Iteration 11 -- 150 151Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 152int(0) 153 154-- Iteration 12 -- 155 156Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 157int(1) 158 159-- Iteration 13 -- 160 161Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 162int(1) 163 164-- Iteration 14 -- 165 166Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 167int(1) 168 169-- Iteration 15 -- 170 171Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 172int(1) 173 174-- Iteration 16 -- 175 176Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 177int(1) 178 179-- Iteration 17 -- 180 181Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 182int(1) 183 184-- Iteration 18 -- 185 186Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 187int(1) 188 189-- Iteration 19 -- 190 191Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 192int(1) 193 194-- Iteration 20 -- 195 196Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 197int(1) 198 199-- Iteration 21 -- 200 201Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 202int(1) 203 204-- Iteration 22 -- 205 206Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 207int(0) 208 209-- Iteration 23 -- 210 211Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 212int(0) 213 214-- Iteration 24 -- 215 216Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d 217int(1) 218Done 219