Dear Readers. In this article we will discuss 4 PHP functions to convert case of letter one by one.
- strtoupper
- strtolower
- ucfirst
- ucwords
What is string ?
A string is a collection/sequence of characters. String is one of the data types supported by PHP. The string variables can contain alphanumeric characters. Strings are created when; You declare variable and assign string characters to it. example: $str = “This is string variable”;
Note: only text can be converted: number or special symbol can’t effect anything.
PHP strtoupper() Function
strtoupper() takes one string parameter, and returns that string entirely in uppercase:
syntax
strtoupper("This is uppercase test")
Example 1: strtoupper function in php
<?php
$string = "My name is Pradip Mehta";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted in uppercase text is :<b>".strtoupper($string)."</b>";
?>
Also, Read
- String reverse without using library function in php
- swap two variables without using 3rd variable
- what is expldoe and implode | explode vs implode
- string replace function in php
Example 2: strtoupper function in php
<?php
$string = "My name is Pradip Mehta @1991";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted in uppercase text is :<b>".strtoupper($string)."</b>";
?>
PHP strtolower() Function
strtolower() takes one string parameter, and returns that string entirely in lowercase:
syntax
strtolower("This is lowercase test")
<?php
$string = "My name is Pradip Mehta";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted in lowercase text is :<b>".strtolower( $string)."</b>";
?>
PHP ucfirst() Function
The ucfirst() function converts the first character of a string to uppercase
syntax
<?php
echo ucfirst("this is ucfirst function in php");
?>
<?php
$string = "this is ucfirst function in php";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted :<b>".ucfirst( $string)."</b>";
?>
PHP ucwords() Function
The ucwords() function converts the first character of each word in a string to uppercase.
syntax
<?php
echo ucwords("this is ucfirst function in php");
?>
<?php
$string = "this is ucfirst function in php";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted :<b>".ucwords( $string)."</b>";
?>
Note: If you want to work on with non-Latin UTF-8 text (δρασκελίζει) then you have to use in prefix mb e.g. mb_strtoupper for convert the text into uppercase similarly for other function you can use. for more details click here.