The reason it is doing that is the order in which PHP is processing your operators.

It processes !mb_strlen($string, 'utf-8') first, so if the length is non-zero that will return true.

It then evaluates true <= 30, which is always true...

So essentially the only case your statement will be false is if a zero length string is given...

See the other answers for how you should write the statement.

Answer from Jacob on Stack Overflow
🌐
PHP
php.net › manual › en › function.mb-strlen.php
PHP: mb_strlen - Manual
If you need length of string in ... use <?php mb_strlen($string, '8bit'); ?>. It's the fastest way (still a way slower than strlen, though) to determine byte length of string....
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-mb_strlen-function
PHP mb_strlen() Function - GeeksforGeeks
April 13, 2023 - Example 1: The following program demonstrates the mb_strlen() function. ... <?php $str = "안녕하세요"; $length = mb_strlen($str, 'UTF-8'); echo "The length of the string is $length"; ?>
🌐
TutorialsPoint
tutorialspoint.com › php-get-the-string-length-using-mb-strlen
PHP – Get the string length using mb_strlen()
August 23, 2021 - In PHP, multibyte string length (mb_strlen) function is used to get the total string length of a specified string. This function is supported in PHP 4.6.0 or higher versions. Syntax
🌐
W3training School
w3trainingschool.com › home › articles › difference between strlen() and mb_strlen() functions in php
Difference Between Strlen() And Mb_strlen() Functions In Php - W3training School
November 11, 2021 - These string functions are used to determine the length of a given string. Function strlen() is used when we require the string length showing a number of bytes whereas function mb_strlen() is used when we require the string length […]
🌐
Code.mu
code.mu › en › php › manual › string › mb_strlen
The mb_strlen Function - Getting String Length in PHP
The mb_strlen function returns the number of characters in a string, working correctly with multibyte encodings (for example, UTF-8). Unlike strlen, it correctly counts characters that occupy multiple bytes. It takes the string as the first parameter and the encoding as the second (optional) one.
🌐
W3Schools
w3schools.com › php › func_string_strlen.asp
PHP strlen() Function
<?php echo strlen("Hello"); ?> Try it Yourself » · The strlen() function returns the length of a string. strlen(string) Return the length of the string "Hello World": <?php echo strlen("Hello world!"); ?> Try it Yourself » · ❮ PHP String ...
🌐
OnlinePHP
onlinephp.io › mb-strlen › manual
mb_strlen - OnlinePHP.io Example
Execute mb_strlen Online. Info and examples on mb_strlen PHP Function from Multibyte String - Human Language and Character Encoding Support
Find elsewhere
Top answer
1 of 3
22

To make it short: you do not really know about the encoding (character set) used on the variables that are passed to your PHP script via GET or POST (especially GET is a problem here). By convention browsers POST forms to the server-side resource specified in the action-attribute using the page encoding which can be specified via an http-equiv-meta-tag (charset-meta-tag in HTML5) or via an HTTP header. Alternatively some browsers also respect the accept-charset-attribute on the form when chosing the correct encoding.

The encoding of GET parameters and the URL itself depends on the browser stettings and can therefore be controlled by the user. You should not rely on a specific encoding.

Generally you'll circumnavigate most encoding-related problems by consistently using UTF-8 for everything and by specifying the correct encoding in the HTTP-header (Content-Type: text/html; charset=UTF-8) - this will yield the correct encoding (UTF-8) in all the variables that are passed into your string (we're not talking about rouge scripts that deliberately try to mess with the encoding to allow for some attack vectors into your script). You also should not rely on non-ascii-characters in your GET parameters or in the URL (that's also a reason why SEO-friendly links remove those characters or substitute them).

If you made sure that UTF-8 is the only allowed character-set you can use mb_strlen($string, 'UTF-8') to check the length of a variable for example.

EDIT: (added some links)

Some things for you to read:

  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
  • Handling UTF-8 with PHP
2 of 3
5

use mb_internal_encoding to know which encoding is currently set. If you application use a log of different encoding you have better to use mb_strlen.

Cheers

🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php strlen
PHP strlen(): Get the Length of a String in Bytes Examples
April 7, 2025 - Use the PHP strlen() function to get the number of bytes of a string. Use the PHP mb_strlen() function to get the number of characters in a string with a specific encoding.
🌐
YouTube
youtube.com › codecourse
The strlen function vs mb_strlen (and others) in PHP - YouTube
Want more? Explore the library at https://www.codecourse.com/lessonsOfficial sitehttps://www.codecourse.comTwitterhttps://twitter.com/teamcodecourse
Published   March 15, 2014
Views   5K
🌐
Illinois State Museum
museum.state.il.us › ismdepts › library › linuxguides › php › function.mb-strlen.html
mb_strlen
mb_strlen() returns number of characters in string str having character encoding encoding.
🌐
OnlinePHP
onlinephp.io › mb-strlen
mb_strlen - Online Tool
PHP Functions · Multibyte String · mb_strlen · Execute mb_strlen with this online tool mb_strlen() - Get string length · Mb Strlen Online Tool · Manual · Code Examples · Mb Strlen Online Tool · Manual · Code Examples · mb_check_encoding · mb_chr · mb_convert_case ·
🌐
Coderwall
coderwall.com › p › qgeuna › php-string-length-the-right-way
[PHP] String Length, The Right Way (Example)
August 26, 2019 - However I disagree with your second assertion, the best way to compare string length in PHP is the obvious way. By making a micro-optimisation such as this early, you lose a communicative aspect to your code. $str = 'hello!'; if (isset($str[5)) { // ... $str is less than or equal to 5 } if (mb_strlen($str) <= 5) { // ...
🌐
Quora
quora.com › What-is-difference-between-mb_strlen-and-strlen-in-PHP
What is difference between mb_strlen() and strlen() in PHP? - Quora
Answer (1 of 10): Ascii characters are 1 byte in length. [code ]strlen[/code] function assumes that every character is 1 byte in length, which works fine for English language. But when you start dealing with non English languages, characters can take more than 1 byte to represent. The number of ...
🌐
WEB sandbox.ws
sandbox.ws › en › php-functions › strlen
Execute PHP strlen and mb_strlen Online: String Length Funct
Using mb_strlen: To calculate the length of a multibyte string, paste your text into the designated input and click the "Calculate Length with mb_strlen" button.
🌐
Reintech
reintech.io › blog › phps-strlen-function-practical-guide-measuring-string-length
PHP's `strlen()` Function: A Practical Guide for Measuring String Length
// UTF-8 encoded string with Chinese characters $greeting = "Hello, 世界!"; echo strlen($greeting); // Output: 15 (bytes) echo mb_strlen($greeting); // Output: 11 (characters) // Breaking down the byte allocation: // "Hello, " = 7 bytes (ASCII) // "世" = 3 bytes (UTF-8) // "界" = 3 bytes (UTF-8) // "!" = 1 byte (ASCII) // Total: 14 bytes