This tutorial will teach you step-by-step how to capitalize the first letter in PHP, with examples.
There are times during the development process when we need to make a string of uppercase, lowercase, or camel-case letters. PHP has many built-in functions that make the first letter capital.
And we are going to show you three methods on how to capitalize the first letter of a string in PHP. Before we begin, if you missed any of our prior lessons, you can review the topics in our PHP Tutorial for Beginners.
The following are the methods to capitalize the first letter in PHP:
Using ucfirst() function
The ucfirst()
function converts the first character of a string to uppercase.
Syntax
ucfirst( string )
Parameters
Parameter | Description |
---|---|
string | Specifies the string that should be converted. |
Example 1
Below is the example of how to use the ucfirst()
function.
<?php
$str = "itsourcecode";
echo ("Original: ".$str);
echo ("<br>");
echo ("Capitalized: ".ucfirst($str));
?>
Output
Original: itsourcecode
Capitalized: Itsourcecode
Example 2
If we wish to convert only the first letter of a string to uppercase and the rest to lowercase, we can use the strtolower()
function in combination with the ucfirst()
function.
<?php
$str = 'itSourceCode';
echo ("Original: ".ucfirst($str));
echo "<br>";
echo ("Capitalize: ".ucfirst(strtolower($str)));
?>
Output
Original: ItSourceCode
Capitalize: Itsourcecode
Using chr() function
The chr()
function returns a character from the specified ASCII value.
Syntax
chr( ascii )
Parameters
Parameter | Description |
---|---|
| An ASCII value |
- Step 1: The initial index can be used to retrieve the first character of a string;
str[0]
returns the first character of the input string. - Step 2: Using the
ord()
function and a negative of32
from the character’s ASCII value, the retrieved character can be transformed into upper case. The uppercase character’s resulting ASCII value is subsequently transformed into a character using thechr()
method. - Step 3: The
substr()
method returns a part of the original string between the start and end indexes.
The substr()
method can be used to get the string from the second character to the end of the string. The final string is made by adding the first character that was changed to upper case to the substring that was found.
Example
<?php
// This is the declaration of string
$str = "where source code is not a problem";
echo ("Original: ".$str);
echo ("<br>");
// This is to get the first character
$gfc = $str[0];
// This is converting the first character to uppercase
$conv_upper = chr(ord($gfc)-32);
// This is to append the first remaining string to upper case character
$str2 = $conv_upper.substr($str,1);
print("Capitalized: ".$str2);
?>
Output
Original: where source code is not a problem
Capitalized: Where source code is not a problem
Using strtoupper() function
The strtoupper()
function converts a string to uppercase.
Based on PHP official documentation, strtoupper()
makes a string uppercase.
Syntax
strtoupper( string )
Parameters
Parameter | Description |
---|---|
| Specifies the string that should be converted. |
- Step 1: The initial index can be used to retrieve the first character of a string;
str[0]
returns the first character of the input string. - Step 2: Using the
strtoupper()
method, which accepts a string as input and changes it to upper case, the character taken from the string can be transformed to upper case. Since a character is also a string, it can be passed to this method as input. - Step 3: In this example, the
substr()
method can be used to extract from the second character of the string to the string’s length. Concatenating the initial character changed to upper case with the resulting substring produces the final string.
Example
<?php
// This is the declaration of string
$str = "where source code is not a problem";
echo ("Original: ".$str);
echo ("<br>");
// This is to get the first character
$gfc = $str[0];
// This is converting the first character to uppercase
$conv_upper = strtoupper($gfc);
// This is to append the first remaining string to upper case character
$str2 = $conv_upper.substr($str,1);
print("Capitalized: ".$str2);
?>
Output
Original: where source code is not a problem
Capitalized: Where source code is not a problem
Frequently Asked Questions
The ucwords()
function changes to uppercase the first letter of each word in a string.
Note: This function is binary-safe.
The strtoupper()
method is one of the most extensively used functions in PHP for converting strings to uppercase. It accepts a string as an argument and converts all lowercase characters to uppercase. Other characters in the string, such as numerals and special characters, stay unchanged.
To capitalize the first character of a string, we can separate it using the charAt() method and then capitalize it using the toUpperCase()
function.
Related Articles
- PHP Elvis Operator (Detailed Explanation With Examples)
- PHP Comment (Detailed Explanation with Examples)
- How to Check a Value Is Numeric in PHP
- PHP Capitalize First Letter with Best Example
- Not in Array PHP (With Advanced Program Examples)
Summary
In summary, the best way to capitalize the first letter in PHP is by using the ucfirst()
function.
Lastly, if you want to learn more about Types of Loops in PHP, please leave a comment below. We’ll be happy to hear it!