Try the common syntax instead:

if (strlen($message)<140) {
    echo "less than 140";
}
else
    if (strlen($message)>140) {
        echo "more than 140";
    }
    else {
        echo "exactly 140";
    }
Answer from nicola on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.strlen.php
PHP: strlen - Manual
PHP's strlen function behaves differently than the C strlen function in terms of its handling of null bytes ('\0'). In PHP, a null byte in a string does NOT count as the end of the string, and any null bytes are included in the length of the string.
๐ŸŒ
W3Schools
w3schools.com โ€บ pHP โ€บ func_string_strlen.asp
PHP strlen() Function
โฎ PHP String Reference ยท Return the length of the string "Hello": <?php echo strlen("Hello"); ?> Try it Yourself ยป ยท Share Link Copied ยท The strlen() function returns the length of a string. strlen(string) Return the length of the string ...
Discussions

Check string length in PHP - Stack Overflow
I have a string that is 141 characters in length. Using the following code I have an if statement to return a message if the string is greater or less than 140. libxml_use_internal_errors(TRUE); $... More on stackoverflow.com
๐ŸŒ stackoverflow.com
PSA! strlen() does not get the length of the characters in a string, it gets the length of bytes in a string. You can use mb_strlen() in combination with a character encoding to get the character length.
Unfortunately, the mb_* functions don't consistently follow Unicode best practices when it comes to dealing with invalid byte sequences. Let's take the sequence "\xe8\x80\\" for example. This sequence is invalid, because the first byte indicates a three-byte sequence, but the third byte is an ASCII backslash instead of a continuation byte. When a UTF-8 parser reads the first byte, it thinks, "Okay, this'll be a three-byte sequence. Codepoint bits: 1000. Looks good so far." Then, it would read the second byte and think, "Okay, continuation byte. Additional codepoint bits: 000000. Looks good so far." It then reads the third byte and thinks, "Uh oh, this was supposed to be a continuation byte, but it isn't. This is invalid." At this point, a parser following best practices should take the first byte of the sequence, as well as any subsequent bytes that are "valid so far", and interpret that sequence as if it were a Unicode Replacement Character (U+FFFD, or ๏ฟฝ), and then behave as if the next byte begins a new sequence. In this case, the three-byte string above would be interpreted as a ๏ฟฝ followed by a backslash. Two characters long. PHP's mb_* functions don't do this consistently. The behavior seems to depend on which function you're using. For example, when mb_strlen() encounters an error anywhere in a three-byte sequence, it seems to behave as if all three bytes were replaced with a replacement character. So, mb_strlen() says the string is only one character long, because it wiped out the backslash. Similarly, mb_strlen("\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0") returns 2, whereas a parser following best practices would return 8. There's a reason I used a backslash character in my first example: Carelessly wiping out bytes like this can have security ramifications. That's exactly why the best practices are defined as they are in the Unicode standard. And then there's this kind of inconsistent behavior: mb_strpos("a\xe8ab", 'b', 0, 'utf-8') === 3 mb_strpos("a\xe8\x80ab", 'b', 0, 'utf-8') === 3 mb_strpos("a\xe8\x80\x80ab", 'b', 0, 'utf-8') === 3 mb_strpos("a\xe8\x80\x80\x80ab", 'b', 0, 'utf-8') === 3 mb_strpos("a\xe8\x80\x80\x80\x80ab", 'b', 0, 'utf-8') === 3 mb_substr("a\xe8\x80\x80\x80\x80ab", 3, 1, 'utf-8') === "\x80" mb_substr("a\xe8\x80\x80\x80\x80ab", 5, 1, 'utf-8') === 'b' wat. It looks like mb_strpos() isn't even trying to parse the data. It's just counting the number of non-continuation bytes before the first match of the string. I'm sure that's more efficient, but I sure hope nobody is using this stuff on anything that hasn't already been properly scrubbed of invalid sequences in advance. More on reddit.com
๐ŸŒ r/PHP
58
104
September 18, 2018
How to find string length in php with out using strlen()? - Stack Overflow
How can you find the length of a string in php with out using strlen() ? More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the maximum length of a String in PHP? - Stack Overflow
So how big can a $variable in PHP get? I've tried to test this, but I'm not sure that I have enough system memory (~2gb). I figure there has to be some kind of limit. What happens when a string More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-find-string-length-in-php.php
How to Find String Length in PHP
The strlen() function return the length of the string on success, and 0 if the string is empty. Let's take a look at the following example to understand how it actually works: ... <?php $str1 = 'Hello world!'; echo strlen($str1); // Outputs: ...
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 8ob9jfdb โ€บ php-string-length-example
How can I get the length of a string in PHP?
To get the length of a string in PHP, use the strlen($string) built-in function. The strlen() takes a string as an argument and returns the length of the string. The strlen() function returns the number of bytes, not characters.
๐ŸŒ
Udemy
blog.udemy.com โ€บ home โ€บ it & development โ€บ web development โ€บ the php strlen function: getting the php string length
The PHP STRLEN Function: Getting the PHP String Length - Udemy Blog
April 14, 2026 - So, the PHP string โ€œHello, World!โ€ is an array of 13 characters, starting with the number 0. The built-in function PHP STRLEN returns the number of characters in the string. ... So, you use STRLEN any time you need the length of a string.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ psa! strlen() does not get the length of the characters in a string, it gets the length of bytes in a string. you can use mb_strlen() in combination with a character encoding to get the character length.
r/PHP on Reddit: PSA! strlen() does not get the length of the characters in a string, it gets the length of bytes in a string. You can use mb_strlen() in combination with a character encoding to get the character length.
September 18, 2018 - Given the voting on this topic, one of PHP core function's behavior is a big surprise for the majority of r/php subscribers. I just can't believe my eyes. Waiting for "PSA! Earth is spherical" topic to make it to the front page. ... And mb_strlen doesn't get you the number of characters in a string either. mb_strlen just returns the number of code points in a string. To get the actual number of chracters, use grapheme_strlen. ... But don't use this to test for length before inserting in a database, since databases measure length in code points or bytes (depending on field type).
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_string.asp
PHP Strings
In PHP, strings are surrounded by either double quotes, or single quotes.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-strlen-function
PHP strlen() Function - GeeksforGeeks
July 11, 2025 - The strlen() is a built-in function in PHP which returns the length of a given string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-get-string-length-in-php
How to get String Length in PHP ? - GeeksforGeeks
July 23, 2025 - ... <?php // PHP program to count all // characters in a string $str = "GeeksforGeekslover"; // Using strlen() function to // get the length of string $len = strlen($str); // Printing the result echo $len; ?>
Top answer
1 of 6
110

http://php.net/manual/en/language.types.string.php says:

Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)

In PHP 5.x, strings were limited to 231-1 bytes, because internal code recorded the length in a signed 32-bit integer.


You can slurp in the contents of an entire file, for instance using file_get_contents()

However, a PHP script has a limit on the total memory it can allocate for all variables in a given script execution, so this effectively places a limit on the length of a single string variable too.

This limit is the memory_limit directive in the php.ini configuration file. The memory limit defaults to 128MB in PHP 5.2, and 8MB in earlier releases.

If you don't specify a memory limit in your php.ini file, it uses the default, which is compiled into the PHP binary. In theory you can modify the source and rebuild PHP to change this default value.

If you specify -1 as the memory limit in your php.ini file, it stop checking and permits your script to use as much memory as the operating system will allocate. This is still a practical limit, but depends on system resources and architecture.


Re comment from @c2:

Here's a test:

<?php

// limit memory usage to 1MB 
ini_set('memory_limit', 1024*1024);

// initially, PHP seems to allocate 768KB for basic operation
printf("memory: %d\n",  memory_get_usage(true));

$str = str_repeat('a',  255*1024);
echo "Allocated string of 255KB\n";

// now we have allocated all of the 1MB of memory allowed
printf("memory: %d\n",  memory_get_usage(true));

// going over the limit causes a fatal error, so no output follows
$str = str_repeat('a',  256*1024);
echo "Allocated string of 256KB\n";
printf("memory: %d\n",  memory_get_usage(true));
2 of 6
17

String can be as large as 2GB.
Source

๐ŸŒ
Functions-Online
functions-online.com โ€บ strlen.html
test strlen online - PHP string functions - functions-online
The function strlen() returns the length of the given $string. ... @mjb4: the charset is a general problem. PHP uses ISO, this site UTF-8.
๐ŸŒ
Learnsic
learnsic.com โ€บ blog โ€บ the-php-strlen-function-getting-the-php-string-length
The PHP STRLEN Function: Getting the PHP String Length
This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy ยท Got it
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ phps-strlen-function-practical-guide-measuring-string-length
PHP's `strlen()` Function: A Practical Guide for Measuring String Length
For standard ASCII strings, strlen() provides accurate character counts since each character occupies exactly one byte: $username = "admin_user"; $length = strlen($username); echo $length; // Output: 10 // Validating input length if (strlen($password) < 8) { throw new Exception("Password must be at least 8 characters"); } // Checking empty strings $input = trim($_POST['email']); if (strlen($input) === 0) { $errors[] = "Email address is required"; }
๐ŸŒ
Kubernetes
kubernetes.io โ€บ docs โ€บ concepts โ€บ configuration โ€บ configmap
ConfigMaps | Kubernetes
November 21, 2025 - Unlike most Kubernetes objects that have a spec, a ConfigMap has data and binaryData fields. These fields accept key-value pairs as their values. Both the data field and the binaryData are optional. The data field is designed to contain UTF-8 strings while the binaryData field is designed to contain binary data as base64-encoded strings.
๐ŸŒ
Claude Platform Docs
platform.claude.com โ€บ messages โ€บ structured outputs
Structured outputs - Claude Platform Docs
enum (strings, numbers, bools, or nulls only - no complex types; see Invalid outputs for a capitalization caveat) ... If you use an unsupported feature, you'll receive a 400 error with details. ... Simple regex patterns work well. Complex patterns may result in 400 errors. ... The Python, TypeScript, Ruby, and PHP SDKs can automatically transform schemas with unsupported features by removing them and adding constraints to field descriptions.
๐ŸŒ
Nextcloud
nextcloud.com โ€บ home โ€บ changelog
Nextcloud changelog
April 12, 2022 - Fix: Remove deprecated RFC7231 constant to avoid warnings on PHP 8.5 (server#58201) Fix: obey x-nc-scheduling flag on delete (server#58203) Remove external shares from share list (server#58204) Fix: show configuration options for external storage backends (server#58205) Fix(snowflake): cast lastId to string (server#58206) Feat: improve VerifyMountPointEvent event (server#58207) Chore: update `@nextcloud/files` to v4.0.0-rc.3 (server#58208) Fix(preview): Fix scanning preview (server#58209) Fix(preview): Handle unique constraints (server#58216) Fix(user_status): use getFirstDay() from @nextcloud
๐ŸŒ
Google
developers.google.com โ€บ recaptcha โ€บ recaptcha v2
reCAPTCHA v2 | Google for Developers
// The id of the reCAPTCHA widget is assigned to 'widgetId1'. widgetId1 = grecaptcha.render('example1', { 'sitekey' : 'your_site_key', 'theme' : 'light' }); widgetId2 = grecaptcha.render(document.getElementById('example2'), { 'sitekey' : 'your_site_key' }); grecaptcha.render('example3', { 'sitekey' : 'your_site_key', 'callback' : verifyCallback, 'theme' : 'dark' }); }; </script> </head> <body> <!-- The g-recaptcha-response string displays in an alert message upon submit.
๐ŸŒ
PHP Freaks
forums.phpfreaks.com โ€บ php coding โ€บ php coding help
[SOLVED] PHP max length of a string? - PHP Coding Help - PHP Freaks
September 8, 2009 - What is the maximum number of characters a string can contain in PHP? The reason I'm asking is because I've created code that creates an output buffer for all HTML code output to browser.