Students Empire

Learn Something New

Learn PHP

PHP String Element

We can accessing characters within a PHP string with [] operator.

We can access the individual characters of a string. To access a character at a particular position, or index , within a string, use:

                    $stringVariable= "a string value";
              $character = $stringVariable[index];   

We place the index between square brackets after the string variable name. String indices start from 0, so the first character in the string has an index of 0, the second has an index of 1, and so on.

You can use [x] notation with strings to read or write individual characters.

                $mystr = ""; 
                $mystr[0] = "H"; 
                $mystr[12] = "!"; 
                print $mystr;