In php, we can capitalize the first letter of a string easily with the php ucfirst() function.

$variable = "this is a string variable";

echo ucfirst($variable);

//Output:
This is a string variable

When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of the variables. One such change is if you want to capitalize the first letter of a string.

To make the first character of a string uppercase in php, we can use the php ucfirst() function.

ucfirst() capitalizes the first character of a string.

Below is an example of how to use ucfirst() to convert the first character of a string to uppercase.

$variable = "this is a string variable";

echo ucfirst($variable);

//Output:
This is a string variable

Capitalizing the First Letter of Each Word with ucwords() Function in php

If you are looking to capitalize the first letter of every word of a string in php, you can use the ucwords() function.

ucwords() capitalizes the first character of each word in a string. To use ucwords() pass a string and an optional delimiter.

Below is an example of how to use ucwords() to convert the first character of each word in a string to uppercase.

$variable = "this is a string variable";

echo ucwords($variable);

//Output:
This Is A String Variable

Converting an Entire String to Uppercase in php with strtoupper() Function

If you instead want to convert all characters in a string to uppercase, we can make a string uppercase easily in php.

The strtoupper() function takes in a string and makes all of the characters uppercase.

Below is an example of how to convert a string uppercase in php with strtoupper().

$variable = "this is a string variable";

echo strtoupper($variable);

//Output:
THIS IS A STRING VARIABLE

Hopefully this article has been useful for you to learn how to capitalize the first letter of a string in php.

Categorized in:

PHP,

Last Update: February 26, 2024