php

Change the string into uppercase 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.

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.

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

";
?>

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.

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

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.

//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);
?>

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.

Middle mouse button not working in Windows 10
The middle mouse button helps you scroll through long webpages and screens with a lot of data. If that stops, well you will end up using the keyboard ...
How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...