How to convert the first character to uppercase using PHP ? - GeeksforGeeks

21 May, 2021
A string is a combination of words combined together. The first character of the string can be converted to an upper case in case it is a lower case alphabetic character. 
Approach 1 : Using chr() method
Step 1: The first character of a string can be extracted using the first index, str[0] returns the first character of the input string. 
Step 1: The first character of a string can be extracted using the first index, str[0] returns the first character of the input string. 
Step 2: The character extracted from the string can be converted to upper case, using the ord() function and then subtracting 32 from its ASCII value. The resultant ASCII value of the upper case character is then converted to a character using the chr() method.
Step 2: The character extracted from the string can be converted to upper case, using the ord() function and then subtracting 32 from its ASCII value. The resultant ASCII value of the upper case character is then converted to a character using the chr() method.
Step 3: The substr() method is used to return a substring of the original string from the start to the end index. 
Step 3: The substr() method is used to return a substring of the original string from the start to the end index. 
The substr() method can be used to extract from the second character of the string till the length of the string. The first character converted to the upper case can then be concatenated to the obtained substring to get the final string. 
Example:
PHP
<?php  //declaring string  $str = "how!to?code? in geeksforgeeks";  print("Original String : ".$str."\n<br/>"); //getting first char  $ch = $str[0];//converting to upper case  $upper = chr(ord($ch)-32);//appending the first remaining string to upper case character  $fin_str = $upper.substr($str,1);  print("Modified String : ".$fin_str);?>
Original String : how!to?code? in geeksforgeeks
Modified String : How!to?code? in geeksforgeeks
Approach 2 : Using ucfirst() method
The ucfirst() method takes an input string and converts its first character to the uppercase, if it is a lower case alphabetic character. The output has to be saved in a variable to retain the changes. 
ucfirst ( str )
Example:
PHP
<?php       $str = "how!to?code? in geeksforgeeks";  print("Original String : ".$str."\n<br/>");   $upper = ucfirst($str);  print("Modified String : ".$upper);?>
Original String : how!to?code? in geeksforgeeks
Modified String : How!to?code? in geeksforgeeks
Approach 3 : Using strtoupper() method
Step 1: The first character of a string can be extracted using the first index, str[0] returns the first character of the input string.
Step 1: The first character of a string can be extracted using the first index, str[0] returns the first character of the input string.
Step 2: The character extracted from the string can be converted to the upper case, using the strtoupper() method, which takes input as the string and converts it to the upper case. Since a character is also a string it can be fed as input in this method.
Step 2: The character extracted from the string can be converted to the upper case, using the strtoupper() method, which takes input as the string and converts it to the upper case. Since a character is also a string it can be fed as input in this method.
Step 3: The substr() method, in this case, can be used to extract from the second character of the string to the length of the string. The first character converted to the upper case can then be concatenated to the obtained substring, to get the final string. 
Step 3: The substr() method, in this case, can be used to extract from the second character of the string to the length of the string. The first character converted to the upper case can then be concatenated to the obtained substring, to get the final string. 
Example:
PHP
<?php  //declaring string  $str = "how!to?code? in geeksforgeeks";  print("Original String : ".$str."\n<br/>"); //getting first char  $ch = $str[0];//converting to upper case  $upper = strtoupper($ch);//appending the first remaining string to uppercase character  $fin_str = $upper.substr($str,1);  print("Modified String : ".$fin_str);?>
Original String : how!to?code? in geeksforgeeks
Modified String : How!to?code? in geeksforgeeks
PHP-function
PHP-Questions
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
Different ways for passing data to view in Laravel
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
How to generate PDF file using PHP ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills