PHP ucfirst() Function

What is PHP ucfirst() Function?

If you want to convert the first character of an ASCII string to uppercase, use ucfirst() function. More specifically, it only modify the first byte of the string.

Syntax:

ucfirst(string)

Parameters:

The Function has 1 parameter which is required-

string (Required): It specifies a string.

Return Values:

The function returns a new converted string which has the first character uppercased.

Notes on ucfirst() Function:

  • The function doesn’t modify the original string. It returns the modified string as a new string. Check example below –
    <?php
    $string = "schools of web";
    ucfirst($string);
    echo "After running the function, it returns: " . $string;
    ?>
    

    Output:

    After running the function, it returns: schools of web
  • If the first character is already uppercased, the function makes no change of the string. Check example below –
    <?php
    $string = "Schools of web";
    echo ucfirst($string);
    ?>
    

    Output:

    Schools of web
  • The function is binary-safe. This function considers its input as byte. It handles binary data (i.e. non-ASCII bytes, UTF-8) correctly. Check example below-
    <?php
    $string = "\x73chools of web";
    $convertedString = ucfirst($string);
    echo "The original string is: " . $string . "<br />";
    echo "After running the function, the original string becomes: " . $convertedString;
    ?>
    

    Output:

    The original string is: schools of web
    After running the function, it returns: Schools of web
  • The function works only on ASCII lowercase characters- a-z. It has no effect on the following characters-
    • Non ASCII characters or multi byte characters.
    • Numbers.
  • As the function has no effect on multi byte characters, you can use mb_ucfirst() function or mb_convert_case() function for multi byte characters.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP ucfirst() Function

PHP’s ucfirst() function is a built-in string function. Use this function to make first letter uppercase of an ASCII characters.

Reference:

https://www.php.net/manual/en/function.ucfirst.php