Changing String Case in PHP

Introduction

I am describing changing string case in PHP. For that I am describing one by one the important string() functions strtolower(), strtoupper(), ucfirst() and ucwords().

First I am discussing the Strtolower() function in PHP. The "Strtolower()" function is a string function. This function converts string uppercase to lowercase.

Syntax

strtolower(string, $str);

Example

<?php

$str = "Hii.. Sharad, How are you?";

$str = strtolower($str);

echo $str;

?>

Output

strtolower function in php.jpg

The "Strtoupper()" function is a string function. This function converts a lowercase string to uppercase.

Syntax

strtoupper(string, $str);

Example

<?php

$str = "Hii.. Sharad, How are you?";

$str = strtoupper($str);

echo $str;

?>

Output

strtoupper function in php.jpg

The "Ucfirst()" function is a string function. This function converts the first character of a string to lower case. For example your first character is lower case so the ucfirst function converts it to a capital letter. For example, I am using the string "hello sir".

Syntax

Ucfirst(string, $str);

Example

<?php

$string1 = 'hello ram!';

$string2 = 'HELLO RAM!';

$str1 = ucfirst($string1);            

$str2 = ucfirst(strtolower($string2));

echo "$str1"."<br>";

echo "$str2";

 

?>

 

Output


Ucfirst function in php.jpg 

The "Ucwords()" function is a string function. This function converts the first character of every word in the string to uppercase. For example, I am using the string "hello shyam!".

 

Syntax

 

Ucwords(string, $str);

 

Example

 

<?php

$string1 = 'hello shyam!';

$string2 = 'HELLO SHYAM!';

$str1 = ucwords($string1);            

$str2 = ucwords(strtolower($string2));

echo "$str1"."<br>";

echo "$str2";

 

?>

 

Output


Ucwords function in php.jpg

 

Up Next
    Ebook Download
    View all
    Learn
    View all