Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

PHP Cookbook: Controlling Case

Posted on February 18, 2012

Problem
You need to capitalize, lowercase, or otherwise modify the case of letters in a string. For example, you want to capitalize the initial letters of names but lowercase the rest.

Solution
Use ucfirst( ) or ucwords( ) to capitalize the first letter of one or more words, as shown in Example 1-25.

Example 1-25. Capitalizing letters
[sourcecode language=”php”]
<?php
print ucfirst("how do you do today?");
print ucwords("the prince of wales");
?>
[/sourcecode]

Example 1-25 prints:
How do you do today?
The Prince Of Wales

Use strtolower( ) or strtoupper( ) to modify the case of entire strings, as in Example 1-26.

Example 1-26. Changing case of strings
print strtoupper("i'm not yelling!");
// Tags must be lowercase to be XHTML compliant
print strtolower('one');

Example 1-26 prints:
I'M NOT YELLING!
one

Discussion
Use ucfirst( ) to capitalize the first character in a string:
[sourcecode language=”php”]
<?php
print ucfirst(‘monkey face’);
print ucfirst(‘1 monkey face’);
?>
[/sourcecode]

This prints:
Monkey face
1 monkey face

Note that the second phrase is not “1 Monkey face.”
Use ucwords( ) to capitalize the first character of each word in a string:

This prints:
1 Monkey Face
Don’t Play Zone Defense Against The Philadelphia 76-ers

As expected, ucwords( ) doesn’t capitalize the “t” in “don’t.” But it also doesn’t capitalize the “e” in “76-ers.” For ucwords( ), a word is any sequence of nonwhitespace characters that follows one or more whitespace characters. Since both ‘ and – aren’t whitespace characters, ucwords( ) doesn’t consider the “t” in “don’t” or the “e” in “76-ers” to be word-starting characters.

Both ucfirst( ) and ucwords( ) don’t change the case of non-first letters:

This prints:

MacWorld says I should get an iBook
ETunaFish.com Might Buy ItunaFish.Com!

The functions strtolower( ) and strtoupper( ) work on entire strings, not just individual characters. All alphabetic characters are changed to lowercase by strtolower( ) and strtoupper( ) changes all alphabetic characters to uppercase:

This prints:

i programmed the wopr and the trs-80.
“SINCE FEELING IS FIRST” IS A POEM BY E. E. CUMMINGS.

When determining upper- and lowercase, these functions respect your locale settings.

See Also
For more information about locale settings, see Chapter 19; documentation on ucfirst( ) at http://www.php.net/ucfirst, ucwords( ) at http://www.php.net/ucwords, strtolower( ) at http://www.php.net/strtolower, and strtoupper( ) at http://www.php.net/strtoupper.

Terbaru

  • Cara Mengatasi Error 208 BCA Mobile
  • Hapus Sekarang! Inilah Hornet, App LGBTQ Tembus Indonesia, Hati-hati
  • Berapa Lama Sih Ngecas HP 33 Watt? Ini Dia Penjelasannya!
  • Cara Cek Saldo Hana Bank Lewat SMS
  • Update Sistem Google Februari 2026: Apa yang Baru dan Perlu Kalian Tahu?
  • Membership FF Bulanan & Mingguan: Berapa DM yang Harus Kalian Siapkan?
  • Maksimal Ngecas HP Berapa Kali Sehari? Boleh 2 Kali Nggak, Sih?
  • Cara Mengatasi Error “Try Again, Open in Another App”
  • Sideload Android: Cara Pasang APK Tanpa Google Play Store (Panduan Lengkap 2026)
  • iPhone Jadi Kamera Vintage Modular? Proyek Kickstarter Ini Bikin Kagum!
  • Cara Mengatasi Video Nest Cam Bermasalah dan Video Hilang
  • iTunes Masih Jadi Rajanya Music? Ini Faktanya!
  • Google Home Smart Button Makin Canggih: Kini Otomatisasi Lebih Fleksibel!
  • F1: The Movie Raih Grammy! Tak Terduga, Kalahkan Bintang Country Ternama
  • Blokir Situs Judi Online: Lindungi Diri & Keluarga dari Dampak Negatif
  • Belanda Larang Anak di Bawah 16 Tahun Gunakan Media Sosial: Ini Detailnya!
  • Gemini Live Mendapat Desain Ulang ‘Floating UI’ yang Super Keren!
  • Galaxy S26 Kehilangan Magnet Qi2? Kebocoran Terbaru Bikin Penasaran!
  • DCT Coin: Crypto Legitim atau Penipuan? Bedah Tuntas, Harga & Fakta Penting!
  • Apple Rilis Update Terbaru untuk iOS, macOS, watchOS & Lainnya!
  • Paket Super Seru Telkomsel: Aplikasi Apa Saja yang Bisa Digunakan?
  • Deus Ex: Human Revolution GRATIS di Android! Buruan Download Sebelum Hilang!
  • Apa Bedanya Thigh, Wing, dan Drumstick di McDonald’s? Yuk, Kupas Tuntas!
  • OpenAI Luncurkan Codex App untuk macOS: Kode Jadi Lebih Mudah!
  • Dana Kaget Wealth Plan: Untung atau Mitos? Review Jujur & Cara Daftarnya!
  • Desain Sony WF-1000XM6 Bocor: Desain Baru, Performa Lebih Gahar!
  • Cara Pinjam 5 Juta di Pegadaian: Mudah, Cepat & Aman! (2024)
  • AYANEO Pocket S Mini: Konsol Android Super Ringkas Hadir!
  • Cara Mendapatkan Kode Verifikasi Telegram Lewat WA
  • Kenapa PayLater Lazada Gak Bisa Dipakai Padahal Udah Lunas?
  • How to Disable AI Features in Firefox 148
  • Git 2.53: What’s New?
  • Linux From Scratch Ditches Old System V init
  • How to Maintained the SSD with TRIM
  • What is CVE-2024-21009? Microsoft Office Security Serious Bug
  • Cara Membuat Podcast dari PDF dengan NotebookLlama dan Groq
  • Tutorial Membuat Sistem Automatic Content Recognition (ACR) untuk Deteksi Logo
  • Apa itu Google Code Wiki?
  • Cara Membuat Agen AI Otomatis untuk Laporan ESG dengan Python dan LangChain
  • Cara Membuat Pipeline RAG dengan Framework AutoRAG
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

©2026 emka.web.id | Design: Newspaperly WordPress Theme