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
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. For example, in PHP: strlen( "te\0st" ) = 5 In C, the same call would return 2.
🌐
W3Schools
w3schools.com › php › func_string_strlen.asp
PHP strlen() Function
zip_close() zip_entry_close() ... zip_entry_read() zip_open() zip_read() PHP Timezones ... The strlen() function returns the length of a string....
Discussions

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
How do you handle long strings in respect to the 120 char limit in PSR-2?

I go one of two ways:

  1. HEREDOC isn't that bad, the indenting thing is a pain but not that much of one. This is usually what I do for strings with significant line breaks (like SQL statements).

  2. Fuck line lengths and just have one long string. It's 2015, we're not on TRS-80s anymore. This is what I do most of the time.

More on reddit.com
🌐 r/PHP
28
19
May 3, 2015
The state of PHP unicode in 2019
PHP doesn't really manage unicode at all. They tried, and that was one of the factors that led to PHP 6 never becoming a thing. So instead, they decided to not have unicode strings at all - you only get byte arrays (which you may write as string literals). If you want actual strings, you have to implement most of it yourself, PHP only gives you a couple of primitives that you can use to operate on various string encodings (including utf-8 and other Unicode encodings) at the byte array level. So basically much the same deal as in C, except that PHP is supposed to be a high-level programming language that takes care of these things for you. More on reddit.com
🌐 r/lolphp
60
27
June 24, 2019
How to remove numbers from end of the string
howdy jishnutp, this seems to do the job ... PM(.*)\d{1,}$ it finds PM and any final digits, then grabs anything between them. it works with your data set on regex101.com if you enable "multiline". take care, lee More on reddit.com
🌐 r/regex
7
3
June 26, 2017
🌐
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: ...
🌐
W3Schools
w3schools.com › php › php_string.asp
PHP Strings
In PHP, strings are surrounded by either double quotes, or single quotes. ... Note: There is a difference between double quotes and single quotes in PHP. You can use double or single quotes, but you should be aware of the differences between the two.
Find elsewhere
🌐
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.
🌐
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.
🌐
Reintech
reintech.io › blog › phps-strlen-function-practical-guide-measuring-string-length
PHP's `strlen()` Function: A Practical Guide for Measuring String Length
The strlen() function is a fundamental PHP string manipulation tool that returns the number of bytes in a string.
🌐
Learnsic
learnsic.com › blog › the-php-strlen-function-getting-the-php-string-length
The PHP STRLEN Function: Getting the PHP String Length
May 6, 2024 - The strlen() function is a built-in PHP function that returns the length of a given string.
🌐
3D Bay
clouddevs.com › home › php guides › php’s strlen() function: measuring string lengths
PHP's strlen() Function: Measuring String Lengths
December 27, 2023 - The length of this string is 13 characters, including spaces and punctuation. When working with multibyte character encodings like UTF-8, strlen() may not give you the expected results because it counts bytes, not characters.
🌐
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.
🌐
Altorouter
altorouter.com › home › php string mastery: unleash power with length manipulation
String Length PHP: A Comprehensive Guide
January 22, 2024 - Mastering string length functions is crucial whether you’re validating user input, truncating text, or performing complex string operations. PHP’s simplest method of determining how long a string is is strlen(). This function returns the number of characters in a given string.
🌐
Tutorialspoint
tutorialspoint.com › php › php_function_strlen.htm
PHP - strlen() Function
It returns the length of the given string − · <?php $str = "Hello from TP"; echo "The given string: $str"; echo "\nThe length of the given string: "; #using strlen() function echo strlen($str); ?>
🌐
TutorialKart
tutorialkart.com › php › php-string-length
How to get Length of String in PHP?
May 7, 2023 - To get the length of a string in PHP, you can use strlen() function.
🌐
Tutorials Class
tutorialsclass.com › home › faqs › career › how to find string length in php?
How to find String Length in PHP? - FAQs - Tutorials Class
April 27, 2020 - The PHP strlen() function returns the length of a string. String length count will include space & special characters as well. See example here
🌐
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; ?>
🌐
Pi My Life Up
pimylifeup.com › home › how to use the php strlen() function
How to use the PHP strlen() Function - Pi My Life Up
June 22, 2022 - It is important to note that the strlen() function will return the string’s length in bytes. So, if you have characters that are multiple bytes (Some UTF-8 characters), you might not get the correct count that you are expecting.