What is PHP mb_ucfirst() Function?
If you want to convert first character (no matter whether it is single byte or multi byte character) of a string to uppercase, use PHP mb_ucfirst() function. So, this function converts the string “schools of web” to “Schools of web”.
When you should use mb_ucfirst() function?
If your string has a character which is not an English character, then you’ll use this function. Because, non-English characters are multi byte characters. And the functions starting with “mb_” can handle those characters correctly.
Syntax:
mb_ucfirst(string, encoding)
Parameters:
The Function has 1 required parameter and 1 optional parameter-
string (Required): It specifies a string whose first character you want to convert to uppercase.
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 (See exercise 3). You can find the default character encoding system in php.ini file in “default_charset = “ setting.
Return Values:
The function returns the input string with its first character converted to uppercase.
Examples:
Example 1:
<?php
$string = "schools of web";
echo mb_ucfirst($string);
?>
Output:
Schools of web
Example 2:
<?php
$string = "écoles du web";
echo mb_ucfirst($string, "UTF-8");
?>
Output:
Écoles du web
Explanation:
The string “écoles du web” is in French. The function mb_ucfirst() correctly converts the first character “é” to “É”.
Notes on mb_ucfirst() Function:
Both ucfirst() and mb_ucfirst() functions can also convert the first character to uppercase of a string. But, ucfirst() function can’t do it if the character is a multi byte character.
PHP Version Support:
PHP 8 >= 8.4.0
Summary: PHP mb_ucfirst() Function
The mb_ucfirst() is a built-in multi byte string function in PHP. You can safely use this function to convert first character of a string (in any language) to uppercase.