How to get String Length in PHP
You can get the length of a string in PHP using the built-in
strlen() utility. It takes one required argument, a string, and returns its length.
Let's try this out on a couple of strings and see what the output is.
$str = 'text'; $result = strlen($str); dd($result);
If the string is empty
0 will be returned by
$str = ''; $result = strlen($str); dd($result);
Note - if anything other than a string is supplied an exception will be thrown.
You now know how to use the
strlen() utility to get string lengths in PHP.