Capitalize first letters of a string with php

Asked by: joseph-brandt
Viewed: 450
Answers: 1
  • 0

How can I make the first letters of each word in a string uppercase in PHP?


Answer by: ChristianKovats

Answered on: 21 Jul 2023

  • 0

You can use the ucwords() function which is a built-in PHP function that converts the first character of each word in a string to uppercase and the rest to lowercase. It’s similar to ucfirst(), which only capitalizes the first character of the first word in a string.

function capitalizeWords($string) {
    return ucwords($string);

Then use the function like this:

$string = "hello world";
$capitalizedString = capitalizeWords($string);
echo $capitalizedString; // Outputs: "Hello World"

Please log in to post an answer!