Dougy Mak's Blog

MY BLOG!

[PHP] Uppercase & Lowercase

Another few functions for I find useful:

  • ucfirst() – Make a string’s first character uppercase
  • lcfirst() – Make a string’s first character lowercase
  • strtolower() – Make a string lowercase
  • strtoupper() – Make a string uppercase
  • ucwords() – Uppercase the first character of each word in a string

From the looks of things, ucfirst() was coined from it’s function uppercase first letter (unlike some functions!). So goes for lcfirst() and ucwords(): lowercase first letter and uppercase first letter of each word. strtolower() lowercases every letter in every word in a string and strupper() functions in opposite, uppercases each letter in every word in a string.

ucfirst() examples:

<?php
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!

$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>

lcfirst() examples:

<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo); // helloWorld


$bar = 'HELLO WORLD!';
$bar = lcfirst($bar); // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
?>

strtolower() examples:

<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>

strtoupper() examples:

<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>

ucwords() examples:


<?php
$foo = 'hello world!';
$foo = ucwords($foo); // Hello World!


$bar = 'HELLO WORLD!';
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>



No comments

No comments yet. Be the first.

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

L:) ;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)