If you have ever asked what is the result of strlen hello world in php, the short answer is 11. In the phrase "hello world", there are 10 visible letters and one space character between the two words, which brings the total length to 11. Practically speaking, the PHP strlen() function counts every byte in a string, including letters, numbers, punctuation, and spaces. Understanding this result is essential for anyone learning PHP string manipulation, form validation, or data processing.
What Is the Result of strlen("hello world") in PHP?
When you run the following code in PHP:
The output is:
11
This happens because strlen() does not only count alphabetic characters. It measures the total number of bytes in the string. The string "hello world" consists of:
- h (1 byte)
- e (1 byte)
- l (1 byte)
- l (1 byte)
- o (1 byte)
- [space] (1 byte)
- w (1 byte)
- o (1 byte)
- r (1 byte)
- l (1 byte)
- d (1 byte)
Adding those together gives you 11 bytes. Many beginners expect the result to be 10 because they only count the visible letters, but the space is a real character in PHP strings.
Understanding the PHP strlen() Function
The strlen() function is a built-in PHP language construct used to determine the length of a string. It accepts one parameter: the string you want to measure. It returns an integer representing the number of bytes in that string.
Here is the basic syntax:
strlen(string $string): int
Good to know here that strlen() operates on a byte level, not a character level. For single-byte encodings such as ASCII or ISO-8859-1, one byte equals one character. That said, for multibyte encodings such as UTF-8, one character can occupy multiple bytes. This distinction becomes critical when working with international text, emojis, or special symbols.
How strlen() Counts Characters
PHP treats strings as sequences of bytes. When you pass "hello world" to strlen(), PHP scans the string from the first byte to the last byte and returns the total count. It does not stop at spaces, and it does not ignore invisible characters.
If you add a tab, newline, or carriage return, strlen() counts those as well. For example:
In this case, the newline character \n adds one extra byte, making the result 12 instead of 11.
The Role of Spaces in String Length
One of the most common points of confusion is whether spaces count toward string length. In PHP, spaces absolutely count. A space character is assigned a byte value (32 in ASCII), and strlen() includes it in the final total.
This means:
strlen("hello")returns 5strlen("hello ")returns 6strlen(" hello")returns 6strlen("hello world")returns 11
If your application logic depends on exact character counts, such as username validation or password strength checks, forgetting to account for spaces can lead to unexpected behavior That's the whole idea..
Code Example: strlen hello world in PHP
Below is a complete, runnable PHP example that demonstrates how to use strlen() with "hello world" and displays the result clearly:
";
echo "The length of the string is: " . $text . $length;
?
When executed in a web browser or command-line interface, the output will be:
The string is: hello world The length of the string is: 11
You can also test variations to see how the result changes:
```php
Why the Result Is 11 and Not 10
The result is 11 because strlen() counts every byte in the string
in the string, including the space between the words. Because both words and the separator are single-byte characters in ASCII, the total comes to 11.
This behavior remains consistent whether the string contains uppercase letters, lowercase letters, numbers, or punctuation. PHP does not apply linguistic rules, context-aware trimming, or character normalization before performing the count That's the part that actually makes a difference. That's the whole idea..
For developers working with multilingual content, relying only on strlen() can become problematic. A string such as "héllo" in UTF-8 may contain more bytes than visible characters, because accented characters and emojis can require multiple bytes. In such cases, the byte count returned by strlen() may not match the actual number of characters a user sees or expects It's one of those things that adds up. And it works..
To count characters rather than bytes, PHP offers the mb_strlen() function from the mbstring extension, which accepts an optional character encoding parameter:
In a nutshell, strlen() returns the number of bytes in a string, not necessarily the number of human-readable characters. That said, it includes spaces, punctuation, and invisible characters such as newlines. For basic ASCII text, this often aligns with the expected character count, but for UTF-8 or other multibyte encodings, it can lead to inaccurate results.
Conclusion: The strlen() function is straightforward and useful for byte-level operations, but developers should understand that it measures bytes. When accurate string length matters for content rendering, validation, or international text, it is usually better to use mb_strlen() or normalize the string before measuring Worth keeping that in mind..
Those distinctions matter in practice. For user-facing limits—display names, comments, labels—measure with the same unit the interface promises. On top of that, a database column defined as VARCHAR(50) usually limits characters, while a byte-oriented check with strlen() may reject valid input too early or allow input that exceeds a downstream byte limit. Likewise, truncating a UTF-8 string with substr() can split a multibyte character and produce invalid output; mb_substr() should be used when operating at character boundaries. For hashes, binary payloads, or fixed-width protocol fields, byte length is exactly the right metric Simple, but easy to overlook..
It is also worth remembering that “character count” can mean different things. mb_strlen() counts code points, which is usually sufficient for validation and storage decisions. For grapheme clusters—such as emoji with skin-tone modifiers or combined accent marks—grapheme_strlen() may better match what a person perceives as a single character. The important point is not that one function is universally superior, but that the measurement should match the requirement.
Final conclusion: strlen() answers a precise question—how many bytes does this string occupy?—and it remains the correct tool for byte-level work. Problems arise only when that byte count is mistaken for visible or semantic length. Use strlen() for binary safety and storage limits expressed in bytes, mb_strlen() for character-aware validation and multilingual text, and grapheme_strlen() when user-perceived characters matter. Choosing the right measure keeps validation, storage, and rendering consistent.
A practical way to lock in these ideas is to centralize the decision. Rather than scattering strlen(), mb_strlen(), and grapheme_strlen() calls throughout an application, wrap them in small helper functions with names that state the intent:
Naming the wrapper after the intent forces a conscious choice at every call site. When someone later reads characterLength($name), the unit is unambiguous, and a reviewer can immediately spot the rare cases where a byte count was used where a character count was meant That's the whole idea..
Performance is rarely a reason to prefer one function over another here. Practically speaking, all three operate in linear time, and mb_strlen() and grapheme_strlen() add only a small constant factor for decoding. The cost of a validation bug—a truncated emoji, a rejected legitimate name, a payload that overflows a byte limit downstream—far outweighs the microseconds saved by reaching for strlen() out of habit. Optimize for correctness first, and reach for byte counting only when bytes are genuinely what you mean Simple as that..
Honestly, this part trips people up more than it should.
A final habit worth building is to make encoding explicit everywhere it matters. And passing 'UTF-8' to mb_strlen() and mb_substr(), or setting mb_internal_encoding('UTF-8') once at bootstrap, removes a class of bugs that appear only when the default encoding differs between environments. Implicit defaults are the enemy of reproducible behavior across development, staging, and production No workaround needed..
Conclusion: strlen() is not a flawed function—it does exactly what it promises, counting bytes with precision and speed. The mistakes come from interpretation, not implementation. Treat byte length, character length, and user-perceived length as three distinct measurements, each with its own correct tool: strlen() for binary data and byte-based limits, mb_strlen() for multilingual text and character-aware validation, and grapheme_strlen() when what the user sees should match what you count. Match the measure to the requirement, make the encoding explicit, and name your helpers after their intent. Do that, and string length stops being a source of subtle bugs and becomes one more decision you make deliberately.