What is PHP mb_strtoupper() Function?
If you want to a string (including a multi-byte one) to uppercase, use mb_strtoupper() function.
Syntax:
mb_strtoupper(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 uppercase version of the input string.
Examples:
Example 1:
<?php
$string = "schools of web";
echo mb_strtoupper($string);
?>
Output:
SCHOOLS OF WEB
Example 2:
<?php
$str = "ё"; // A Russian small letter
echo mb_strtoupper($str, "UTF-8");
?>
Output:
Ё
Difference between strtoupper() function and mb_strtoupper() function:
- The strtoupper() function only converts single byte characters or ASCII characters. On the other hand, mb_strtoupper() function can converts both single byte or multi byte characters.
- The mb_strtoupper() function is slightly slower than strtoupper().
Best Practices on mb_strtoupper() Function:
While encoding is optional, it’s best practice to explicitly mention its value. Example “UTF-8”.
Notes on mb_strtoupper() Function:
- For modern web applications (especially multilingual ones), prefer mb_strtoupper() over strtoupper() 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().
Difference between PHP strtoupper() and mb_strtoupper() Function:
Both mb_strtoupper() over strtoupper() functions can converts all text to uppercase. But, there are some differences between them-
- strtoupper() function can convert single byte ASCII characters (English) only. On the other hand, mb_strtoupper() function can also convert multi byte characters (non English).
- strtoupper() function is faster than mb_strtoupper() function. mb_strtoupper() function is a bit slower.
- To convert English characters only, use strtoupper() function not mb_strtoupper() function.
- The mb_strtoupper() function requires mbstring extension to run. On the other, strtoupper() 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_strtoupper() Function
The mb_strtoupper() is a built-in multi byte string function in PHP. Use this function to safely convert string of any language to its uppercase.