PHP strlen()
Published Jul 5, 2022Updated Jul 28, 2023
Contribute to Docs
The strlen() method returns the length of a string.
Note: This method returns the number of bytes rather than the number of characters.
Syntax
strlen($string);
The strlen() method takes one required parameter: $string, which can be a literal sequence of characters surrounded by quotes or a variable that refers to such a sequence. The method returns the length of $string in bytes, including all characters, special characters, and whitespaces. It returns 0 if $string is an empty string.
Example
The following example uses the strlen() method to determine the length of a sentence. Then the echo command prints it to the console:
<?phpecho strlen("How many characters are in this sentence?");?>
The example will result in the following output:
41
Codebyte Example
The following example uses the strlen() method in two different ways:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours