The strlen() function in PHP 8.4 and PHP 8.5 has no behavioral changes compared to previous 8.x versions. Its core functionality remains the same: it returns the number of bytes in a string, not the number of characters.
Function
The signature is consistent across PHP 8.4 and 8.5:
strlen(string $string): int
Example (Basic Length):
<?php
$str = abcdef ;
echo strlen($str); // Output: 6
$str = ab cd ;
echo strlen($str); // Output: 7 (spaces are counted)
?>
Behavior: Bytes vs. Characters
This is the most important concept to understand. strlen() counts bytes. For multibyte encodings like UTF-8, a single character can take up multiple bytes, so the result may not match the visual character count.
Example (Multibyte String):
<?php
$utf8 = "café";
// The "é" is 2 bytes in UTF-8
echo strlen($utf8); // Output: 5 (c, a, f, and two bytes for é)
?>
If you need the number of characters, you should use mb_strlen() instead:
<?php
echo mb_strlen("café", "UTF-8"); // Output: 4
?>
Version Specific Considerations (8.4 & 8.5)
While the function itself hasn not changed, here is what you need to know when working with these versions:
Deprecation of null (Since PHP 8.1): Passing null to strlen() is deprecated. While it may still return 0 in 8.4/8.5 (often with a deprecation notice depending on your error reporting settings), you should explicitly check for null or empty strings to write clean code .
PHP 8.5 is New Pipe Operator: PHP 8.5 introduces the |> operator. While not a change to strlen() itself, it offers a new way to call it in a chain:
<?php
// Traditional
$result = strlen("Hello World");
// PHP 8.5 Pipe syntax
$result = "Hello World" |> strlen(...);
?>
Performance: Since PHP 7, strlen() has been extremely fast (essentially constant time, O(1)) because PHP stores string length internally. You do not need to worry about performance when calling it repeatedly.