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

  • Inilah Kenapa KBLI Sangat Penting Buat Bisnis Digital dan Gini Caranya Biar Kalian Nggak Salah Pilih Kode
  • Inilah Fitur Keren ONLYOFFICE Docs 9.3, Cara Baru Edit PDF dan Dokumen Lebih Efisien!
  • Inilah Cara Banjir Komisi Shopee Affiliate Hanya Dalam 7 Hari Saja
  • Cara Download Aplikasi BUSSID Versi 3.7.1 Masih Dicari dan Link Download Aman Mediafire
  • Inilah Cara Lengkap Mengajukan SKMT dan SKBK di EMIS GTK 2026, Biar Tunjangan Cair Tanpa Drama!
  • Inilah Kenapa Bukti Setor Zakat Kalian Harus Ada NPWP-nya, Jangan Sampai Klaim Pajak Ditolak!
  • Inilah Cara Jadi Clipper Video Sukses Tanpa Perlu Tampil di Depan Kamera
  • Inilah Cara Upload NPWP dan Rekening di EMIS GTK Madrasah Terbaru, Jangan Sampai Tunjangan Terhambat!
  • Inilah Fakta di Balik Video Ukhti Sholat Mukena Pink Viral yang Bikin Geger Media Sosial
  • Belum Tahu? Inilah Langkah Pengisian Survei Digitalisasi Pembelajaran 2026 Biar Nggak Salah
  • Ini Kronologi Video Viral Ibu Tiri vs Anak Tiri Ladang Sawit Tiktok
  • Inilah Caranya Update Chromebook Sekolah Agar Siap Digunakan untuk TKA SD dan SMP
  • Inilah Caranya Menghindari Mafia Kontraktor Renovasi Nakal Supaya Budget Nggak Boncos
  • Inilah Trik Rahasia Tembus 4000 Jam Tayang YouTube Tahun 2026 Cuma Dalam 2 Hari
  • Inilah Rahasia Tomoro Coffee Bisa Singkirkan Semua Pesaing dan Ekspansi Massif Banget!
  • Inilah Kenapa AI Sering Jadi Kambing Hitam Saat PHK Massal, Padahal Masalahnya Bukan Itu!
  • Inilah Cara Baru Cepat Monetisasi YouTube Tanpa Trik Musik, Cuma 2 Bulan Langsung Cuan!
  • Inilah Kapan Pengumuman SNBP 2026 Jam Berapa Lengkap dengan Cara Cek Hasil Kelulusannya
  • Belum Tahu? Inilah Cara Zakat Perusahaan Kalian Bisa Jadi Pengurang Pajak Penghasilan Bruto!
  • Inilah Cara Menggabungkan Grid Foto Online dan Hapus Background Foto
  • Kenapa Youtuber Mulai Harus Hati-hati Pakai AI: Bisa Digugat dan Kehilangan Hak Cipta!
  • Inilah Alasan Kenapa Sumbangan ke Tempat Ibadah Biasa Nggak Bisa Langsung Jadi Pemotong Pajak!
  • Belum Tahu? Inilah Cara Pasang Iklan Meta Ads untuk Sales WiFi Supaya Banjir Closingan!
  • Inilah Alur Pengerjaan EMIS GTK 2026 yang Benar dari Awal Sampai Akhir
  • Inilah 27 Sekolah Kedinasan untuk Lulusan SMK 2026, Bisa Kuliah Gratis dan Langsung Jadi CPNS!
  • Inilah Cara Kuliah S2 di Inggris dengan GREAT Scholarship 2026: Syarat Lengkap, Daftar Kampus, dan Tips Jitu Biar Lolos!
  • Belum Tahu? Inilah Alasan Non-Muslim Juga Bisa Ngurangin Pajak Pake Sumbangan Keagamaan Wajib!
  • Inilah Kenapa Zakat ke Pondok Pesantren Mungkin Nggak Bisa Jadi Pengurang Pajak, Yuk Cek Syaratnya!
  • Inilah Caranya Daftar SMA Unggul Garuda Baru 2026 yang Diperpanjang, Cek Syarat dan Link Resminya!
  • Cara Cek Pencairan KJP Plus Tahap 1 Januari 2026 Beserta Daftar Nominal Lengkapnya
  • Is the $600 MacBook Neo Actually Any Good? A Detailed Deep-Dive for Student!
  • Build Your Own Mini Data Center: A Guide to Creating a Kubernetes Homelab
  • How Enterprise Stop Breaches with Automated Attack Surface Management
  • The Roadmap to Becoming a Professional Python Developer in the AI Era
  • Why Your High Linux Uptime is Actually a Security Risk: A Lesson for Future Sysadmins
  • How to Use the Tiiny AI Pocket Lab to Run Local Large Language Models
  • AI SEO Tutorial With OpenClaw, Make Your Website Traffic from 0 to 780 Clicks Daily
  • How to Use SoulX FlashHead To Create The Best Talking Avatar for Free on Google Colab!
  • New Claude AI Memory: How to Seamlessly Sync ChatGPT to Anthropic in Minutes
  • Securing LLM with Agentverse (Secure and Scalable Inference)
  • 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 Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ

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