Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Playing with strings Wish List | Report Bug | About Me ]

4.7.9     Changing string case: strtoupper(), strtolower(), ucfirst(), and ucwords()

This is NOT the latest copy of this book; click here for the latest version.

string strtoupper ( string source)

string strtolower ( string source)

string ucfirst ( string source)

string ucwords ( string source)

Strtoupper() is part of a small family of functions that affect the case of characters of strings. Strtoupper() takes one string parameter, and returns that string entirely in uppercase. Other variations include strtolower(), to convert the string to lowercase, ucfirst() to convert the first letter of every string to uppercase, and ucwords(), to convert the first letter of every word in the string to uppercase. They all take one parameter and return the converted result, so once you learn one you have learnt them all:

<?php
    $string
= "i like to program in PHP";
    
$a = strtoupper($string);
    
$b = strtolower($string);
    
$c = ucfirst($string);
    
$d = ucwords($string);
    
$e = ucwords(strtolower($string));
?>

Each of those variables get set to a slightly different value: $a becomes "I LIKE TO PROGAM IN PHP", $b becomes "i like to program in php", $c becomes "I like to program in PHP", $d becomes "I Like To Program In PHP", and $e becomes "I Like To Program In Php".

From that, you should be able to see that in calls such as ucwords(), PHP will not change existing capital letters to lowercase, which is why $d and $e are different - for $e, all the letters are lowercased first, then passed through ucwords() to make PHP into Php.





<< 4.7.8 Wrapping your lines: wordwrap()   4.7.10 Making a secure data hash: sha1() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Gogo the Great, Kingstown, Serbia - 06 Sep 2008

3rd, January, 2006

I'm back again, after learning a few things about arrays from this gorgeous manual.

@ Tom
Now I realize there's a slight typo in your example. In line:

$return[] ucfirst($val);

the assignment operator is missing. It should read like this:

$return[] = ucfirst($val);

After that correction, everything works perfectly well.
Thank you for this useful function ;)

Gogo the Great - 06 Sep 2008

@ Tom

In my case, it returned the following error:

"Fatal error: Cannot use [] for reading in..."

Suppose I should be able to correct this after learning something about arrays, but I don't wanna jump ahead so much right now ;)

Tom - 06 Sep 2008

To change to sentence case:
<?php
function strtosentence($string) {
$array = explode(". ", $string);
foreach($array as $val) {
$return[] ucfirst($val);
}
$return = join(". ", $return);
return $return;
}
?>
I'm pretty sure that should work.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow