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.

Syntax:

string strtoupper(string $string)

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.

<?php
//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:

string ucfirst(string $string)

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.

<?php

//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:

string ucwords(string $string)

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.

<?php
//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.

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.