strlen()
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:
All contributors
- cslylla58 total contributions
- BrandonDusch580 total contributions
Looking to contribute?
- 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.