note 79531 added to function.count

From: Date: Sun, 02 Dec 2007 12:47:35 +0000
Subject: note 79531 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-132732@lists.php.net to get a copy of this message
Since the square bracket notation works on strings and arrays, I wrote this to use if you don't know whether $input will be a string or an array. <?php // Count elements in an array, properties in an object, or characters in a string function lengthOf($input) { if (is_object($input) || is_array($input)) { return count($input); } else { return strlen((string) $input); } } ?> With this function, this block will work for strings and arrays: <?php for ($i = 0; $i < lengthOf($input); $i++) { echo $input[$i]; } ?> /** Cross Posted (I know, I'm evil) **/ ---- Server IP: 69.147.83.197 Probable Submitter: 207.224.195.213 ---- Manual Page -- http://www.php.net/manual/en/function.count.php Edit -- https://master.php.net/note/edit/79531 Del: integrated -- https://master.php.net/note/delete/79531/integrated Del: useless -- https://master.php.net/note/delete/79531/useless Del: bad code -- https://master.php.net/note/delete/79531/bad+code Del: spam -- https://master.php.net/note/delete/79531/spam Del: non-english -- https://master.php.net/note/delete/79531/non-english Del: in docs -- https://master.php.net/note/delete/79531/in+docs Del: other reasons-- https://master.php.net/note/delete/79531 Reject -- https://master.php.net/note/reject/79531 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132732) next »