PHP strlen(): Get the Length of a String in Bytes Examples (original) (raw)

Skip to content

Summary: in this tutorial, you’ll learn how to use the PHP strlen() function to get the length of a string.

Introduction to the PHP strlen() function #

The strlen() function returns the length of a specified string. Here’s the syntax of the strlen() function:

strlen ( string $string ) : intCode language: PHP (php)

The strlen() function has one parameter $string, which is the string to measure the length. The strlen() function returns the length of the $string in bytes or zero if the $string is empty.

It’s important to note that the strlen() function returns the number of bytes rather than the number of characters. If each character is 1 byte, the number of bytes is the same as the number of characters.

However, if you deal with the multibyte string, e.g., UTF-8, the number of bytes is higher than the number of characters.

To get the number of characters in a multibyte string, you should use the mb_strlen() function instead:

mb_strlen ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi mathvariant="normal">∣</mi><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">string , string|null </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">∣</span><span class="mord mathnormal">n</span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span></span></span></span>encoding = null ) : intCode language: PHP (php)

The mb_strlen() function has an additional $encoding that specifies the character encoding of the $string.

The mb_strlen() function returns the number of characters in the $string having character $encoding. The mb_strlen() returns one for each multibyte character.

PHP strlen() function examples #

Let’s take some examples of using the strlen() function.

1) Simple strlen() function example #

The following example uses the strlen() function to return the length of the string PHP:

<?php $str = 'PHP'; echo strlen($str); // 3Code language: PHP (php)

Try it

2) Using the strlen() with a multibyte string #

The following multibyte string has five characters. However, its size is 15 bytes.

'こんにちは'Code language: PHP (php)

By the way, こんにちは is a greeting in Japanese. It means hello in English.

The strlen() function returns 15 bytes for the string 'こんにちは':

`<?php

$message = 'こんにちは'; echo strlen($message); // 15 bytes `Code language: PHP (php)

Output:

15 Code language: PHP (php)

But the mb_strlen() function returns five characters for that string:

`<?php

$message = 'こんにちは'; echo mb_strlen($message); // 5 characters`Code language: PHP (php)

Output:

5Code language: PHP (php)

Summary #

Did you find this tutorial useful?