Accessing String Offsets

chinmay.sahoo

New member
Individual characters in a string can be accessed using the $str{offset} notation. You can use it to both read and write string offsets. When reading characters, this notation should be used only to access valid indices. When modifying characters, you may access offsets that don’t yet exist. PHP automatically sets that offset to the said character, and if this results in a gap between the ending of the original string and the offset of the new character, the gap filled with space characters (' ').


This example creates and prints the string "Andi" (in an awkward way):

$str = "A";
$str{2} = "d";
$str{1} = "n";
$str = $str . "i";
print $str;
 
Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character. Note: As of PHP 7.1.0, negative string offsets are also supported. These specify the offset from the end of the string.

Best Astrology Services in Canada | Vashikaran Services in Canada | Spiritual Healer in Canada | Top Indian Astrologer in Toronto | Indian Astrologer in Toronto | Indian Astrologer in Canada | Best Astrologer in Canada
 
Back
Top