php

Uppercase first character of each word of a String in PHP

If you need to uppercase the first character of each word in a string in PHP, you can use the ucwords() function. This function takes a string as an argument and returns a new string with the first character of each word in the original string capitalized.

<?php
  $str = "hello world";
  
  $res = ucwords($str);
  
  echo $res;
?>

Output

Hello World

To convert the first letter of each word of a String to uppercase:

  1. Define a String that contains some words.
  2. Pass the string to the ucwords() function that will return the string with each word having the first character uppercase.
  3. print the string.

Syntax

ucwords($str)
Was this helpful?