- Php all first letter uppercase
- Change the string into uppercase in PHP
- Use of strtoupper()
- Example 1: Check the authentication using strtoupper()
- Valid user.
- Invalid user.
- Required argument value(s) is/are missing.
- Use of ucfirst()
- Example 2: Convert the first letter of a sentence into uppercase
- Use of ucwords()
- Example 3: Convert the first letter of each word of the sentence
- The original string is:
- The converted string is:
- Conclusion
- Video Tutorial
- About the author
- Fahmida Yesmin
- PHP ucfirst() Function
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
Php all first letter uppercase
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
Change the string into uppercase in PHP
PHP has many built-in functions to change the case of the string. The string value can be converted into all uppercase or lowercase; convert the first letter of the string into the uppercase or lowercase, and convert the first character of each word of a string into uppercase. strtoupper(), ucfirst(), and ucwords() functions are used to change the case of a full string or a part of a string into the uppercase letter in different ways. The uses of these functions have been explained in this tutorial by using different examples.
Use of strtoupper()
This function is used to convert all characters of a string onto uppercase. The syntax of this function is given below.
This function takes a string value as the argument and returns the content of the string after converting all letters into uppercase.
Example 1: Check the authentication using strtoupper()
It is a common task of any web application to check the username and password to validate the users. The following example shows the use of the strtoupper() function to authenticate the user. No HTML form is used in the script to take the username and password. The user and password values will be provided using URL query strings. isset() function is used to check if the $_GET[‘user’] and $_GET[‘password’] variables are initialized or not. Next, the trim() function is used to remove the extra space from the data that is retrieved from the query string. strtuupper() function will convert the values of $username and $password for comparing $username with ‘ADMIN’ and $password with ‘QWE789’ to validate the user.
//Check the required query string values are set or not
if ( isset ( $_GET [ ‘user’ ] ) && isset ( $_GET [ ‘password’ ] ) )
{
//Set the username and password
$username = trim ( $_GET [ ‘user’ ] ) ;
$password = trim ( $_GET [ ‘password’ ] ) ;
//Check the validity of the user by converting the user and password values into uppercase
if ( strtoupper ( $username ) == ‘ADMIN’ && strtoupper ( $password ) == ‘QWE789’ )
{
echo «
Valid user.
» ;
}
else
{
echo «
Invalid user.
» ;
}
}
else
//Print the error message
echo «
Required argument value(s) is/are missing.
» ;
?php>
Output:
The following output will appear if no query string is provided in the URL.
The following output will appear if the correct values are provided for user and password parameters.
The following output will appear if the incorrect values are provided for user and password parameters.
Use of ucfirst()
This function is used to convert the first character of a string only. If the string contains multiple sentences, then the ucfirst() function will change the first character of the first sentence only. The syntax of this function is given below.
This function takes a string value as the argument and returns the content of the string after converting the first character of the first sentence of the string into uppercase.
Example 2: Convert the first letter of a sentence into uppercase
The following example shows the way to change the first letter of each sentence into the uppercase of multiline string data. The first ucfirst() function is used to change the first letter of a single sentence into uppercase. The second ucfirst() function is applied to the string of multiline sentences, and it will change the first letter of the first sentence into uppercase only. Next, each sentence of the multiline string is separated using the explode() function, and the third ucfirst() function is used to convert the first letter of each sentence into uppercase.
//Set the string of the single sentence
$string = «javaScript is a client-side programming language.» ;
echo «The output of ucfirst() for the single sentence:
» . ucfirst ( $string ) . «
» ;
//Set the string of multiple sentences
$string = «html is a mark-up language to design a web page. the tags used
in HTML script are predefined. it can only display the static data.» ;
echo «
The output of ucfirst() for the multiple sentences:
» . ucfirst ( $string ) . «
» ;
//Convert the first letter of each sentence of the string
$str_arr = explode ( ‘.’ , $string ) ;
$result = «» ;
foreach ( $str_arr as $value ) {
$result .= ucfirst ( trim ( $value ) ) . ‘.’ ;
}
$result = substr ( $result , 0 , strlen ( $result ) — 1 ) ;
echo «
The output of the string after converting the first character of each sentence:
» . $result ;
Output:
The following output will appear after running the script from the server. In the first output, ‘javaScript’ has been converted into ‘JavaScript’. In the second output, ‘html’ has been converted into ‘Html’, and other sentences have remained unchanged. In the third output, ‘html’, ‘the’, and ‘it’ has been converted into ‘Html’, ‘The’, and ‘It’.
Use of ucwords()
This function is used to convert the first letter of each word of the string. The syntax of this function is given below.
This function takes a string value as the argument and returns the content of the string after converting the first letter of each word of the sentence into uppercase.
Example 3: Convert the first letter of each word of the sentence
The following example shows the use of the ucword() function to convert the first letter of each word of multiple words in string data into uppercase. A string variable named $string is defined in the script that contains a string of three words. The script will print the original string and the converted string after applying the ucword() function.
//Set the string value
$string = «welcome to linuxhint» ;
echo »
The original string is:
» ;
//Print the original string
echo $string ;
echo »
The converted string is:
» ;
//Print the converted string
echo ucwords ( $string ) ;
?>?php>
The following output will appear after running the script from the server. It shows that the ‘welcome to linuxhint’ string is converted into ‘Welcome To Linuxhint’ after using the ucwords() function.
Conclusion
Different types of built-in functions exist in PHP to change the content of the string data in multiple ways. The three uppercase-related functions have been explained in this tutorial using three examples. These functions are used to change all letters of a string, the first character of the string, and the first letter of each word of the string into uppercase. PHP has another function named strtolower() that will convert all letters of a string into lowercase.
Video Tutorial
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
PHP ucfirst() Function
The ucfirst() function converts the first character of a string to uppercase.
- lcfirst() — converts the first character of a string to lowercase
- ucwords() — converts the first character of each word in a string to uppercase
- strtoupper() — converts a string to uppercase
- strtolower() — converts a string to lowercase
Syntax
Parameter Values
Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.