In PHP, how do I index strings with examples?

In PHP, you can index strings using square brackets or the substr() function. Indexing allows you to access specific characters within a string based on their position, starting from 0 for the first character.

Example of Indexing Strings in PHP

<?php $string = "Hello, World!"; $firstCharacter = $string[0]; // H $secondCharacter = $string[1]; // e echo $firstCharacter; // Outputs: H echo $secondCharacter; // Outputs: e // Using substr() function $extractedSubstring = substr($string, 7, 5); // World echo $extractedSubstring; // Outputs: World ?>

PHP String Indexing Substring Character Access