Friday, October 24, 2025
HomeLanguagesPHP ucfirst() Function

PHP ucfirst() Function

The ucfirst() function is a built-in function in PHP which takes a string as an argument and returns the string with the first character in Upper Case and all other characters remain unchanged.

Syntax:

ucfirst($string)

Parameter: The function accepts only one parameter $string which is mandatory. This parameter represents the string whose first character will be changed to uppercase.

Return Value: The function returns the same string only by changing the first character to upper case of the passed argument $string.

Examples:

Input : "neveropen for neveropen"
Output : Geeks for neveropen

Input : "Chetna Agarwal"
Output : Chetna Agarwal

Below programs illustrate the ucfirst() function in PHP:

Program 1: The program below demonstrates the use of ucfirst() function.




<?php
    // PHP program that demonstrates the 
    // use of ucfirst() function 
      
    $str = "neveropen for neveropen";
      
    // converts the first case to upper case 
    // and prints the string
    echo(ucfirst($str)); 
?>


Output:

Geeks for neveropen

Program 2: The program below demonstrates the use of ucfirst() function when the starting character is in upper-case.




<?php
    // PHP program that demonstrates the 
    // use of ucfirst() function when 
    // the first case is already in uppercase
      
    $str = "Chetna Agarwal";
      
    // already the first character is in upper case 
    // so prints the same string only
    echo(ucfirst($str)); 
?>


Output:

Chetna Agarwal

Reference:
http://php.net/manual/en/function.ucfirst.php

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS