The Wayback Machine - https://web.archive.org/web/20240717024725/https://www.geeksforgeeks.org/how-to-convert-the-first-character-to-uppercase-using-php/
Open In App

How to convert the first character to uppercase using PHP ?

Last Updated : 21 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

How to convert first character of all the words uppercase using PHP ?
To convert the first character of all the words present in a string to uppercase, we just need to use one PHP function i.e. ucwords(). ucwords(string,delimiters)This function takes 2 parameters. The first one is the string which is mandatory. The second parameter is the delimiter. Example 1: [GFGTABS] PHP &lt;?php echo ucwords(&quot;welcome to geek
2 min read
How to convert uppercase string to lowercase using PHP ?
The best way to convert uppercase string to lowercase in PHP is by using the strtolower() function and using mb_strtolower() Function. It takes the string as the input and converts it's all characters to the lower case and returns the string value. Syntax: string strtolower( $string )Return value: It returns the string converted into lower string.
1 min read
How to convert lowercase string to uppercase using PHP ?
A string is a storage object in PHP comprising characters, numbers or special symbols. Strings in PHP are case-sensitive. The interconversion between lower and upper case can be easily done using various in-built methods in PHP. Table of Content Using chr() method Using strtoupper() method Using mb_convert_case() functionUsing preg_replace_callback
4 min read
How to convert a normal string to a uppercase string using filter in VueJS ?
Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is the one that’s actually printed in the Vue.js templ
2 min read
How to convert LowerCase values to UpperCase in Input Field using ReactJS ?
In React, to convert the Lower case input values to uppercase we can use the Javascript methods like toUpperCase and toLocaleUppercase methods along with the event listeners. Prerequisites:React JSNPM &amp; Node.jsJavaScript toUpperCase methodApproach:To convert LowerCase values to UpperCase in the Input Field using React JS we will be using the Ja
2 min read
How to make first letter of a string uppercase in JavaScript ?
In JavaScript, transforming the first letter of a string to uppercase is a common task, crucial for maintaining consistent text formatting and enhancing readability. Utilizing built-in string manipulation functions facilitates this process, ensuring user-friendly display and improving the overall user experience in web applications. Here are some c
4 min read
How to Convert a String to Uppercase in JavaScript ?
In JavaScript, you can convert a string to uppercase using the toUpperCase() method, which is available on string objects. This method returns a new string with all alphabetic characters converted to uppercase. Syntax:string.toUpperCase()string: The string that you want to convert to uppercase.Example: Here, the toUpperCase() method is called on th
1 min read
PHP Change strings in an array to uppercase
Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized. Examples: Input : arr[] = ("geeks", "For", "GEEks") Output : Array ([0]=&gt;GEEKS [1]=&gt;FOR [2]
3 min read
How to change the color of the first character of a word using PHP script?
The preg_replace() function is an in-built function that returns a word or array of words with the replacement of some content after searching the pattern. If the search pattern is not found then it will be returned unchanged. Approach: Declare a variable with a string value.Use preg_replace() function to replace the first character of every word.D
1 min read
How to remove the first character of string in PHP?
Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation: In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. removing characters are to be known. Example: [GFGT
3 min read
three90RightbarBannerImg