PHP strlen() counts the bytes in a PHP string, not Unicode characters. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is strlen() in PHP?
Counts the bytes in a PHP string, not Unicode characters.
Its call structure is:
strlen($string)
The arguments identify the values or resources passed to strlen(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.
Example 1: Basic Usage
This example introduces strlen with a direct call and a small, readable input.
<?php
echo strlen("hello");
Output:
5
Counts the bytes in a PHP string, not Unicode characters. For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how strlen behaves.
<?php
echo strlen("é"); // UTF-8 uses more than one byte.
Output:
2
The changed input illustrates why output may differ with the chosen values or environment. For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes.
Practical Example: Using the Feature
The following example incorporates strlen into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
$payload = "ABC123"; if (strlen($payload) <= 16) { echo "Within byte limit"; }
Output:
Within byte limit
In a real application, check each operation result, validate values before use and clean up acquired resources. For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes.
Important Behavior and Edge Cases
- Purpose: Counts the bytes in a PHP string, not Unicode characters.
- Key limitation: For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes.
- Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.
Comparison With Related Functions
| Function or Approach | Best Fit |
|---|---|
strlen() | Counts the bytes in a PHP string, not Unicode characters. |
mb_strlen() | Counts Unicode code points in a chosen character encoding, unlike byte-counting strlen(). |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes. | The correct approach depends on this behavior: For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes. |
| Ignoring errors or cleanup after the operation. | Validate inputs and check each result; close files, restore settings or release resources when the operation requires it. |
Key Takeaways
- Counts the bytes in a PHP string, not Unicode characters.
- For UTF-8 text, use mb_strlen() to count code points or grapheme_strlen() for visible graphemes.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
strlen is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.