note 22411 added to function.substr
| From: | maxim@php.net | Date: | Mon, 17 Jun 2002 16:06:11 +0000 |
| Subject: | note 22411 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31943@lists.php.net to get a copy of this message | ||
When you need a first element (or another one you know the position of) you can do this:
$str = 'Maxim Maletsky';
echo '<p>The first letter of my name is (' . $str[0] . ')';
// This prints:
// First letter of my name is (M)
//
// This example is the same as substr($str, 0, 1);
---
// Now, the same thing is when you need to find out the last letter:
echo '<p>The last letter of my name is (' . $str[strlen($str)-1] . ')';
// This prints:
// First letter of my name is (y)
//
// This example is the same as substr($str, -1);
---
Now, try this out:
for($i=0; $i<strlen($str); $i++) {
echo "<br> Character [$i] is (" . $str[$i] . ")";
}
This happens because PHP is a C based language, and see thinks this way. Using this instead of
substring when you can will help you a bit not to create too many confusion.
Maxim Maletsky
maxim@php.net
--
http://www.php.net/manual/en/function.substr.php
http://master.php.net/manage/user-notes.php?action=edit+22411
http://master.php.net/manage/user-notes.php?action=delete+22411
http://master.php.net/manage/user-notes.php?action=reject+22411