if (length($str) < $index) { $s=substr($str,$index,1); } else { whatever else }