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:

<?php
echo 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:

Code
Output
Loading...

All contributors

Looking to contribute?

Learn PHP on Codecademy