The strlen()
function
returns the number of characters in a string:
$string
=
'Hello, world'
;
$length
=
strlen
(
$string
);
// $length is 12
You can use the string offset syntax on a string to address individual characters:
$string
=
'Hello'
;
for
(
$i
=
0
;
$i
<
strlen
(
$string
);
$i
++
)
{
printf
(
"The %dth character is %s
\n
"
,
$i
,
$string
{
$i
});
}
The
0
th
character
is
H
The
1
th
character
is
e
The
2
th
character
is
l
The
3
th
character
is
l
The
4
th
character
is
o
Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.