note 79532 added to function.strlen
| From: | egingell at sisna dot com | Date: | Sun, 02 Dec 2007 12:48:07 +0000 |
| Subject: | note 79532 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132733@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.strlen.php
Edit -- https://master.php.net/note/edit/79532
Del: integrated -- https://master.php.net/note/delete/79532/integrated
Del: useless -- https://master.php.net/note/delete/79532/useless
Del: bad code -- https://master.php.net/note/delete/79532/bad+code
Del: spam -- https://master.php.net/note/delete/79532/spam
Del: non-english -- https://master.php.net/note/delete/79532/non-english
Del: in docs -- https://master.php.net/note/delete/79532/in+docs
Del: other reasons-- https://master.php.net/note/delete/79532
Reject -- https://master.php.net/note/reject/79532
Search -- https://master.php.net/manage/user-notes.php