Capitalize first letters of a string with php


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

Answer ( 1 )


    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"

Leave an answer