Introduction

To access a character at a particular position, or index , within a string, use:

$character = $string[index];

You 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 both read and change characters this way.

Here are some examples:

Demo

<?php
$myString = "Hello, world!";
echo $myString[0] . " \n "; 
echo $myString[7] . " \n "; 
$myString[12] ='?';
echo $myString . " \n ";   
?>//from w ww.j  a  v a 2s  . c  o  m

Result

Related Topic