<?php
echo ucfirst("we are programmers");
// -> We are programmers
?>
To convert the first character of the string to uppercase:
Syntax
ucfirst($str)
# Method 2: Using strtoupper() function
If you do not want to use the ucfirst() function then you can also use strtoupper() function. It converts all the characters of the string to uppercase.
We will get the first character of the string and convert it to uppercase. Then replace it with the first character of the string. Check the below example:
<?php
$str = "hello world";
$str[0] = strtoupper($str[0]);
echo $str;
?>
Output
Hello world
In the above code example, we have a string called $str.
We are getting the first letter from the string using $str[0] and then passing it to strtoupper() function that will return the uppercase format of this letter.
Then replace it with the first letter of the string and print it.
0 Comments