All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Uppercase First Letter Of Each Word

Last Updated : Mar 11, 2024

PHP Uppercase First Letter Of Each Word

In this tutorial we will show you the solution of PHP uppercase first letter of each word, sometimes in php we need to apply uppercase on a special line or statement there are two ways to do this.

First is that we can manually write first letter of each word in uppercase or we can use inbuilt function given by php on that statement to display first letter of each word in uppercase.

Let us see the example given below.

Step By Step Guide On PHP Uppercase First Letter Of Each Word :-

Here, manually writing uppercase first letter of each word in php is not a good way to write code for programmer.

So, the best way is to use inbuilt function because if we writing an article or book then it became difficult for us to manually writing each statement, hence we use inbuilt functions there.

Let us see the code given below for better understanding.

<form action=" " method="post">
<label>
 Enter a number between 1 - 26
</label>
<input type="text" name="number">
<input type="submit" name="submit">
</form>
<?php
// simple way of writing uppercase first letter of each word
echo ucwords("hello welcome to talkers code");
// output is Hello Welcome To Talkers Code
// more examples for better understanding
$string = ' hello welcome to talkers code!';
echo ucwords($string);
// output is Hello Welcome To Talkers Code!
$anotherExp = ‘hello welcome to talkers code!';
echo ucwords($anotherExp);
echo ucwords(strtolower($anotherExp));
// OUTPUT IS HELLO WELCOME TO TALKERS CODE!
// output is Hello Welcome To Talkers Code!
//With custom delimiter
$foo = ' hello|welcome|to|talkers|code!';
echo ucwords($foo);
// output is Hello|Welcome|To|Talkers|Code!
?>
  1. Here, above as we see that we give many examples with help of which we are able to uppercase first letter of each word.
  2. The above examples are done with the help of function that is inbuilt function used in php for uppercase each word.
  3. In first example as we see that in this we use ucwords() function. In which we directly give or write the string which we want in uppercase each word. Hence, this is the way with help of which we are able to uppercase first letter of each word.
  4. Now, in series of next example the same function is used again and again, but the way used is different. In first example, as we that we directly use the string inside ucwords() function. but in next we store the required string inside a variable and then apply the function on this.
  5. As, we show many ways to done this on many types of strings and to print the output on display we use echo there.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to uppercase first letter of each word in php.

I hope this tutorial on PHP uppercase first letter of each word helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪