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