String Length in PHP using strlen() Function
In this tutorial, we will learn about string length in PHP. In PHP or any other programming languages, string length (i.e. length of a string) refers to the number of characters it contains.
A string is a sequence of characters that can contain letters, numbers, symbols, and white space. Determining string length is one of the most common operations performed on strings.
By default, PHP considers each byte as one character. This works fine for ASCII or other single-byte encodings, where each character consists of a single byte. However, if you are working with multibyte encodings, like UTF-8, where a character may be composed of multiple bytes, using byte-based functions like strlen() can produce inaccurate results.
To overcome this problem, PHP provides several built-in functions to accurately calculate string length depending on the encoding and the requirement. Let’s understand each built-in function to find the length of string in PHP.
String Length in PHP using strlen() Function
The strlen() function in PHP is a built-in function that is used to determine the length of a string. This is the most common and straightforward method to get the length of a string in PHP. The general syntax to define strlen() function in PHP is as follows:
strlen(string $string): int
In the above syntax, the parameter $string specifies the string whose length you want to determine. This function returns the length of the string, including all the whitespaces and special characters in bytes as an integer. The strlen() function does not count the number of characters.
Basic Examples of strlen() Function
Example 1: Finding length of a simple string
<?php
$str = "Scientech Easy";
// Calculating the length of string.
$strLength = strlen($str);
echo $strLength;
?>
Output: 14
In this example, the string “Scientech Easy” contains 14 characters and all are ASCII (single-byte) characters, so the strlen() function returns 14.
Example 2: Counting length with spaces and special characters
<?php
$str = " PHP 123! ";
echo "Length of string: " . strlen($str);
?>
Output: Length of string: 10
In this example, the string $str has 9 characters, including spaces, numbers, and punctuation. Since they are all ASCII characters (i.e. byte-based), strlen() counts them correctly.
Multibyte Strings in PHP
By default, PHP considers each byte as one character. The strlen() function works perfectly for ASCII or a single-byte encoded string, where each character consists of a single byte.
However, if you are working with multibyte encodings, like UTF-8, where a character may be composed of multiple bytes, then using byte-based functions like strlen() can produce inaccurate results. Look at the below example.
Example 3: Using strlen() with emojis
<?php
$str = "😊";
echo "Length of string: " . strlen($str);
?>
Output: Length of string: 4
In this example, we have used an emoji which is encoded as 4 bytes in UTF-8. Since we have used strlen() function to find the length of the string stored in $str, this function counts the number of bytes, not characters. Since the emoji is encoded as 4 bytes in UTF-8, strlen($str) will return 4.
Although visually “😊” is one character, and takes 4 bytes in UTF-8 encoding. This can cause confusion if you expect strlen() function to return 1. Therefore, if you want to count the actual number of characters, use mb_strlen() function. The general syntax is as:
$length = mb_strlen($string, $encoding);
In the above syntax, the parameter $encoding specifies the character encoding (defaults to internal encoding if not specified).
Example 4:
<?php
$str = "😊";
echo "Length of string emoji: " . mb_strlen($str, 'UTF-8');
?>
Output: Length of string emoji: 1
In this example code, we have used the mb_strlen() function which correctly counts the number of characters, because it understands the multibyte nature of UTF-8 encoding.