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

  • Belum Tahu Apa Itu Store Associate? Inilah Tugas, Tanggung Jawab, dan Bocoran Gajinya Secara Lengkap
  • Inilah Aturan Saldo Minimal BNI Biar Rekening Kalian Nggak Hangus
  • Apakah Screenshot Story WA Ada Notifnya atau Nggak!
  • Inilah Cara Langganan YouTube Music Premium 2026 Khusus Pelajar Agar Kantong Nggak Jebol!
  • Inilah Kumpulan Ide Bio WhatsApp Keren dan Aesthetic Biar Profil Kalian Makin Hits!
  • Sering Ditolak Pinjaman? Ini Trik Supaya Nama Kalian Bersih dari Blacklist BI Checking
  • Cara Munculin Tanda Love Merah di Story IG, Ternyata Ini Rahasianya!
  • Inilah Cara Transfer DANA ke DANA Tanpa Verifikasi KTP yang Praktis dan Aman!
  • 02129187000 Nomor Apa? Simak Penjelasan Lengkap dan Cara Blokirnya Biar Nggak Terganggu Lagi
  • Sering Ditelepon 14010? Jangan Panik, Inilah Penjelasan Lengkap Mengenai Siapa Pemilik Nomor Tersebut!
  • 818 Nomor Apa, Jangan Langsung Dimatiin Kalau XL Lagi Telepon!
  • 1500597 Nomor Telepon Apa? Ini Dia Jawaban Lengkap dan Cara Menghadapinya!
  • 1500877 Nomor Apa? Ternyata Ini Pemilik Nomor yang Sering Telepon Kalian!
  • Cara Berhenti Langganan Melolo Biar Saldo Dana Nggak Terus-terusan Kepotong Otomatis!
  • Inilah Trik Mengatasi Riwayat Pendidikan yang Hilang di Info GTK Supaya Tetap Valid!
  • Inilah Cara Mengaktifkan & Mematikan Opsi Pengembang HP Samsung dengan Mudah!
  • Inilah Jam Berapa Gaji Payroll Masuk ke Rekening dan Kenapa Bisa Telat
  • Caranya Cek Nomor Kartu ATM Mandiri Lewat Email, Praktis Banget Buat Transaksi Online!
  • Gini Caranya Atasi Rombel 0 dan Siswa 0 di Info GTK, Ternyata Nggak Ribet Kok!
  • Coretax 2026: Inilah Alasan Kenapa Bukti Potong A1 Kalian Sekarang Nggak Nihil Lagi
  • Inilah Cara Atasi Masalah Akun dan Sinkron Dapodik 2026 Terbaru
  • Inilah Cara Mengatasi Notifikasi Gagal Didaftarkan Saat Mutasi Dapodik 2026
  • Cara Daftar NPWP Online Lewat HP di CoreTax Khusus Affiliator
  • Inilah Cara Bikin Foto Buram Jadi HD di Remini Web Tanpa Instal Aplikasi
  • Inilah Cara Update Data SIMSarpras 2026 Supaya Madrasah Kalian Cepat Dapat Bantuan!
  • Ini Panduan Ringkas Penyelenggaraan TKA 2026 Dengan Fasilitas Terbatas
  • Mau Tarik Saldo Rp700 Ribu di Free Drama tapi Stuck? Ini Cara Cepat Tembus Level 30!
  • Belum Tahu? Ini Trik Checkout Tokopedia Bayar Pakai Dana Cicil Tanpa Ribet!
  • Benarkah Pinjol Akulaku Sebar Data Jika Gagal Bayar?
  • Paket Nyangkut di CRN Gateway J&T? Tidak Tahu Lokasinya? Ini Cara Mencarinya!
  • How to Secure Your Moltbot (ClawdBot): Security Hardening Fixes for Beginners
  • Workflows++: Open-source Tool to Automate Coding
  • MiroThinker-v1.5-30B Model Explained: Smart AI That Actually Thinks Before It Speaks
  • PentestAgent: Open-source AI Agent Framework for Blackbox Security Testing & Pentest
  • TastyIgniter: Open-source Online Restaurant System
  • 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
  • Contoh Sourcecode OpenAI GPT-3.5 sampai GPT-5
  • 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