PHP convert string first letter case

Reprinted from: http://www.php-note.com/article/detail/140

Convert the first letter of each word to uppercase: ucwords()

 
1<?php
2$foo ='hello world!';
3$foo = ucwords($foo);             // Hello World!
4  
5$bar ='HELLO WORLD!';
6$bar = ucwords($bar);             // HELLO WORLD!
7$bar = ucwords(strtolower($bar));// Hello World!
8?>

 

Capitalize the first letter of the first word: ucfirst()

 
1<?php
2$foo ='hello world!';
3$foo = ucfirst($foo);             // Hello world!
4  
5$bar ='HELLO WORLD!';
6$bar = ucfirst($bar);             // HELLO WORLD!
7$bar = ucfirst(strtolower($bar));// Hello world!
8?>

 

Lowercase the first letter of the first word: lcfirst()

 
1<?php
2$foo ='HelloWorld';
3$foo = lcfirst($foo);             // helloWorld
4  
5$bar ='HELLO WORLD!';
6$bar = lcfirst($bar);             // hELLO WORLD!
7$bar = lcfirst(strtoupper($bar));// hELLO WORLD!
8?>

 

Capitalize all letters: strtoupper()

Lowercase all letters: strtolower()

 

Related: PHP convert string first letter case