Skip to content

ucfirst()

Introduction

The ucfirst() function in PHP is used to capitalize the first character of a string. In this article, we will discuss the ucfirst() function in detail and how it can be used in PHP.

Understanding the ucfirst() function

The syntax for using the ucfirst() function in PHP is as follows:

The PHP syntax of the ucfirst()

php
ucfirst(string $string) : string

Here, $string is the string we want to modify. The ucfirst() function returns the modified string with the first character capitalized.

Example Usage

Here is an example usage of the ucfirst() function in PHP:

Example of PHP ucfirst()

php
<?php

$string = "hello, world!";
$capitalizedString = ucfirst($string);
echo $capitalizedString;

In the example above, we define a string $string with all characters in lowercase. We then use the ucfirst() function to capitalize the first character of the string. Finally, we print out the modified string.

Conclusion

The ucfirst() function in PHP is a useful tool for capitalizing the first character of a string. It can be particularly useful when working with user input, where the first character may need to be capitalized for aesthetic or grammatical reasons. By understanding how to use the ucfirst() function, developers can create more efficient and effective PHP applications.

Practice

What does the PHP ucfirst() function do?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.