Home »
PHP »
PHP Programs
How to convert a string to uppercase in PHP?
Learn, how can we convert a string to uppercase? To convert string to uppercase, we use strtoupper() method which returns uppercase converted string.
By IncludeHelp Last updated : December 19, 2023
Prerequisites
To understand this example, you should have the basic knowledge of the following PHP topics:
PHP - Converting a string to uppercase
To convert a given string to uppercase in PHP, we use strtoupper() method.
strtoupper() Function
This method takes a string in any case as an argument and returns uppercase string.
Syntax
strtoupper(string)
PHP code to convert string into uppercase
<?php
// Declaring a variable and assigning
// string in it
$str = "hello friends";
// Converting to uppercase, and printing it
echo strtoupper($str);
?>
Output
HELLO FRIENDS
Other similar functions
PHP - ucfirst ()
This function converts first letter in uppercase of the string.
Example
<?php
echo ucfirst("hello friend");
//output: Hello friends
?>
PHP - ucwords ()
This function converts each first character of all words in uppercase.
Example
<?php
echo ucwords("how are you");
//Output: How Are You?
?>
Complete code (HTML with PHP)
<html>
<head>
<title> PHP code to get textbox value and print it in Uppercase </title>
</head>
<body>
<form action="">
<input type="text" id="str" type="text" name="str" maxlength="10" size="26">
<input type="submit" name="submit" formmethod="POST">
</form>
<?php
if(isset($_POST['submit']))
{
$str = strtoupper($_POST['str']);
echo "convert to upper case";
echo $str;
}
?>
</body>
</html>
Output
PHP String Programs »