note 79532 deleted from function.strlen by felipe
| From: | felipe@php.net | Date: | Sun, 16 Dec 2007 01:32:59 +0000 |
| Subject: | note 79532 deleted from function.strlen by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133618@lists.php.net to get a copy of this message | ||
Note Submitter: egingell at sisna dot com
----
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) **/