Use of strtoupper()
This function is used to convert all characters of a string onto uppercase. The syntax of this function is given below.
Syntax:
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 "<h3 style='color:green'>Valid user.</h3>";
}
else
{
echo "<h3 style='color:green'>Invalid user.</h3>";
}
}
else
//Print the error message
echo "<h3 style='color:red'>Required argument value(s) is/are missing.</h3>";
?>
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.
Syntax:
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 "<b>The output of ucfirst() for the single sentence:</b><br />".ucfirst($string)."<br />";
//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 "<br /><b>The output of ucfirst() for the multiple sentences:</b><br />".ucfirst($string)."<br />";
//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 "<br /><b>The output of the string after converting the first character of each sentence:</b><br />".$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.
Syntax:
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 "<h4> The original string is: </h4>";
//Print the original string
echo $string;
echo "<h4> The converted string is: </h4>";
//Print the converted string
echo ucwords($string);
?>
Output:
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.