PHP - String ucfirst() Function



The PHP ucfirst() function is used to convert the first character of a given string to uppercase. The term "uppercase" refers to the capital letter characters such as A, B, C....Z.

This function will only convert the first character of the first word in the given string to uppercase, even if the string contains multiple words.

For example, if we have the string "hi aman", the resultant string will be "Hi aman", rather than "Hi Aman".

Syntax

Following is the syntax of the PHP ucfirst() function −

ucfirst(string $str): string

Parameters

Following is the parameter of this function −

  • str − The input string.

Return Value

This function returns a string with first letter in uppercase.

Example 1

The following program demonstrates the usage of the PHP ucfirst() function −

<?php
   $str = "tutorialspoint";
   echo "The original string: $str";
   echo "\nThe resultant string: ";
   #using ucfirst() function
   echo ucfirst($str);
?>

Output

The above program produces the following output −

The original string: tutorialspoint
The resultant string: Tutorialspoint

Example 2

If the first character of the given string is already in uppercase, the original string will not be affected.

Here is another example of using the PHP ucfirst() function. We use this function to convert the first character of this string "Hello World" to uppercase −

<?php
   $str = "Hello World";
   echo "The original string: $str";
   echo "\nThe resultant string: ";
   #using ucfirst() function
   echo ucfirst($str);
?>

Output

After executing the above program, the following output will be displayed −

The original string: Hello World
The resultant string: Hello World
php_function_reference.htm
Advertisements