Uppercase the first character of each word in a string using PHP
You can use ucwords() to uppercase the first character of each word in a string. This is handy for formatting names, titles, or any user input that needs title case.
$myVar = 'this is a test!';
$myVar = ucwords($myVar);
// This Is A Test!
By default, ucwords() treats spaces as word separators. If your words are separated by something else (like hyphens or underscores), you can pass a second argument to specify the delimiter:
$myVar = ucwords('hello-world', '-');
// Hello-World
Related functions: ucfirst() capitalizes only the first character of the entire string, strtolower() converts everything to lowercase, and strtoupper() converts everything to uppercase. A common pattern is to combine strtolower() with ucwords() to normalize inconsistent casing: ucwords(strtolower($input)).