PHP Capitalize First Letter of Each Word
To capitalize the first letter of each word in a PHP string, we need to use the PHP ucwords() in-built function. This function is used to capitalize the first character of every word of a string. If we are in a situation where all the lowercase of the first character of a string change into uppercase, then we use ucword() function.
Why should we Capitalize the First Letter of Each Word
It is generally done for aesthetic and readability purposes and is considered a best practice in programming. If we capitalize the first letter of each word, it makes the text easier to read and understand. This is especially important when working with longer strings, such as titles or descriptions. Additionally, it helps to ensure that variables and functions are named consistently.
Capitalizing the first letter of the word, is a common convention in many programming languages and coding standards, making your code more understandable and familiar to other developers who may be working on the same project.
So, capitalizing the first letter of each word improves the readability, maintainability, and consistency of your code.
2 Ways to capitalize first letter
These are the easiest and most common ways to capitalize the first letter of each word.
Using ucwords() function
Ex:
<!DOCTYPE html>
<html>
<body>
<?php
$str1 = "the name's bond, james bond";
echo ucwords($str1);
?>
</body>
</html>
Output:
In this code:
- We have created the variable ‘str1’ and initialized it to some text in lowercase.
- We need to use the ucwords() function to capitalize the first letter in each word.
- And pass the variable as an argument to the function ucwords().
- Finally, print the variable ‘str1’ using the echo statement. See the above output the text will capitalize the first letter.
Using a loop and strtoupper() function:
We can use a loop to iterate through each word in the string and use the strtoupper() function to capitalize the first letter of each word.
$string = "hello world";
$words = explode(" ", $string);
foreach ($words as &$word) {
$word = strtoupper(substr($word, 0, 1)) . substr($word, 1);
}
$string = implode(" ", $words);
echo $string;
// Output: Hello World
- In this code, we have created the variable ‘string’ and initialized it to some text in lowercase
- Then we created the variable ‘words’ and initialized using explode() function. Then we passed two arguments to this function, one is an empty string (” “) and another is a variable ‘string‘.
- Then we created foreach loop and iterate over each word in the
$words
array. - Within the loop, the
substr()
function is used to extract the first letter of each word (from position 0, with a length of 1), and thestrtoupper()
function is used to convert it to uppercase. - After the loop is complete, the
implode()
function is used to join the modified$words
array back into a single string, using the space character as the separator. - Finally, we print the variable ‘string’ using an echo statement. See the output the variable ‘string’ text is changed to a capital letter successfully