What is PHP mb_strtolower() Function?
If you want to a string (including a multi-byte one) to lowercase, use mb_strtolower() function.
Syntax:
mb_strtolower(string,encoding)
Parameters:
The Function has 1 required parameter and 1 optional parameter-
string (Required): It specifies the input string.
encoding (Optional): It specifies the character encoding system to use. The character encoding system encodes the string. If you omit this parameter or mention it NULL, the default character encoding system will be used. But, the default encoding may not encode the string properly. You can find the default character encoding system in php.ini file in “default_charset = “ setting.
Return Values:
The function returns the lowercase version of the input string.
Examples:
Example 1:
<?php
$string = "SCHOOLS OF WEB";
echo mb_strtolower($string);
?>
Output:
schools of web
Example 2:
<?php
$str = "Ё"; // Russian capital letter
echo mb_strtolower($str, "UTF-8");
?>
Output:
ё
Difference between strtolower() function and mb_strtolower() function:
- The strtolower() function only converts single byte characters or ASCII characters. On the other hand, mb_strtolower() function can converts both single byte or multi byte characters.
- The mb_strtolower() function is slightly slower than strtolower().
Best Practices on mb_strtolower() Function:
While encoding is optional, it’s best practice to explicitly mention its value. Example “UTF-8”.
Notes on mb_strtolower() Function:
- For modern web applications (especially multilingual ones), prefer mb_strtolower() over strtolower() for string case conversion.
- It is safer to use for non-English text conversion.
- If you don’t mention an encoding explicitly, the function uses the internal character encoding set by mb_internal_encoding().
- The function added conditional casing rules for the Greek letter sigma, improving accuracy from PHP version 8.3.
Difference between PHP strtolower() and mb_strtolower() Function:
Both mb_strtolower() over strtolower() functions can converts all text to lowercase. But, there are some differences between them-
- strtolower() function can convert single byte ASCII characters (English) only. On the other hand, mb_strtolower() function can also convert multi byte characters (non English).
- strtolower() function is faster than mb_strtolower() function. mb_strtolower() function is a bit slower.
- To convert English characters only, use strtolower() function not mb_strtolower() function.
- The mb_strtolower() function requires mbstring extension to run. On the other, strtolower() function doesn’t require any extension, it depends on core PHP.
PHP Version Support:
PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8
Summary: PHP mb_strtolower() Function:
The mb_strtolower() is a built-in multi byte string function in PHP. Use this function to safely convert string of any language to its lowercase.