ARTICLE

Changing String Case in PHP

Posted by Vinod Kumar Articles | PHP January 29, 2013
I am describing changing string case in PHP using the important string functions strtolower, strtoupper, ucfirst and ucwords.
Reader Level:

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

 

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter