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);
4
If the string is empty 0
will be returned by strlen()
.
$str = '';
$result = strlen($str);
dd($result);
0
Note - if anything other than a string is supplied an exception will be thrown.
Conclusion
You now know how to use the strlen()
utility to get string lengths in PHP.
string
length