PHP Reference Β· String Functions
PHP ucfirst() function
Reference for PHP ucfirst(): make the first character of a string uppercase, what it does to the rest, multibyte limits and how to capitalize whole words.
ucfirst() returns a copy of a string with its first byte converted to uppercase if it is a lowercase ASCII letter. The rest of the string is left exactly as it was. It is handy for turning a lowercase word or a database value into the start of a sentence.
Because it works on bytes, only a to z change. An accented letter such as Γ© at the start stays unchanged, and an empty string comes back empty.
<?php echo ucfirst("hello world"); // Hello world
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
ucfirst(string $string): string
| Name | Description |
|---|---|
$string |
The input string. |
Return value: the string with its first character uppercased if it is an ASCII lowercase letter, otherwise the string unchanged.
Usage notes
- Only the first byte is touched.
ucfirst("hELLO")givesHELLO; combine withstrtolower()to getHello. - ASCII only. Since PHP 8.2 the function no longer depends on the current locale, so results are the same on every server. For
Γ©,Γ±,ΠΆand other letters usemb_strtoupper()withmb_substr(). - A leading space blocks it.
ucfirst(" hello")changes nothing because the first character is a space;trim()first. - For every word, use
ucwords(). The opposite operation islcfirst(). - Use it as a callback by passing its name as a string, as in
array_map('ucfirst', $words), or with the first-class callable syntaxucfirst(...). - Strings are immutable values. The original variable is not modified; assign the result.
Related
lcfirst(),ucwords(),strtoupper(),strtolower(),mb_strtoupper()- Lessons: Strings, String functions