Skip to content
Related Articles

Related Articles

Improve Article

How to convert the first character to uppercase using PHP ?

  • Last Updated : 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 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. 



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);
?>
Output
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);
?>
Output
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 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. 

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);
?>
Output
Original String : how!to?code? in geeksforgeeks
Modified String : How!to?code? in geeksforgeeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :