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

  • Apa Itu Rasio Gambar Ukuran 1:1 di Canva? Ini Pengertian dan Cara Buatnya
  • Pengiriman Shopee Express Hemat itu Berapa Lama? Ini Pengertian dan Estimasi Sampainya
  • Android 2025: Aplikasi Baru dan Smartphone Impian yang Akan Datang!
  • Apa Itu Google AI Pro Ultra? Ini Pengertian dan Penjelasan Lengkapnya
  • Apa Itu Error Gagal Kirim Nilai RDM 3.1? Ini Pengertian dan Solusi Mengatasinya
  • Facebook Mulai Batasi Link Eksternal Cuma 2 Sebulan! Ini Trik Mengatasinya
  • Cara Nonton Tensura Season 4! Bakal Tayang April 2026 dengan Format 5 Cour
  • Belum Tahu? Inilah Trik Supaya Live TikTok Kalian Aman dan Banjir Cuan
  • Pixel 8 Dapatkan Mode Panorama Baru! Hasil Foto Lebih Luas & Lebih Kreatif
  • Apa Itu AppLocker? Ini Pengertian dan Cara Mengamankan Windows 11 Kalian dari Skrip Jahat
  • Cara Membuat Riwayat Copilot Kamu Tetap Bersih dan Rapi!
  • Game & Aplikasi Android Terbaik Saat Diskon Liburan Natal! Jangan Ketinggalan!
  • Apa Itu Game Prison Escape Journey? Ini Pengertian dan Cara Mainnya untuk Pemula
  • “Listrik Gratis” dari Solar Panel Cuma Mitos?! Ini Sisi Gelap PLTS
  • Samsung Galaxy Z-Fold: Uji Jatuh Bebas yang Mengguncang Keandalan Lipatannya
  • Google One 2026: Apa yang Akan Jadi Fitur Utama dan Harga yang Diharapkan?
  • Apa Itu Error 0x800704f8? Ini Pengertian dan Cara Mengatasinya
  • Android Akhirnya Dapat GPS Darurat di India! Setelah Hampir 10 Tahun
  • Apa Itu GetContact Premium? Ini Pengertian dan Cara Daftarnya
  • Android Maze Figure: Koleksi Baru Google yang Bikin Penggemar Bergairah!
  • Google Update Besar-besaran Desember 2025: Apa yang Akan Memengaruhi Pengalaman Anda?
  • Masih Pusing Hitung Gaji Manual? Waktunya Pakai Aplikasi HR
  • Apa Itu Dustruco? Ini Pengertian dan Cara Pasangnya di HP Kalian
  • Apa Itu Aplikasi Dooie Live? Ini Pengertian dan Cara Pakai Aplikasinya untuk Cari Jodoh
  • Apa Itu Battle Emote Jefri Nichol dan Om Telolet Om di MLBB? Ini Penjelasannya
  • Apa itu Game Luna Mobile dan Bagaimana Cara Menangnya?
  • Apa Itu Kompensasi Sistem Trail Mobile Legends? Ini Penjelasan dan Cara Klaim Hadiahnya
  • Apa Itu Update Google Pixel 2 Desember 2025? Ini Penjelasannya!
  • Ini Cara Reset Desil di Aplikasi Cek Bansos Biar Valid (Update Januari 2026)
  • Apa Itu EZNET Wireless dan Fiber Optic? Ini Perbedaan dan Pengertian Lengkapnya
  • Apa itu Cosmic Desktop: Pengertian dan Cara Pasangnya di Ubuntu 26.04?
  • Apa Itu Auvidea X242? Pengertian Carrier Board Jetson T5000 dengan Dual 10Gbe
  • Elementary OS 8.1 Resmi Rilis: Kini Pakai Wayland Secara Standar!
  • Apa Itu Raspberry Pi Imager? Pengertian dan Pembaruan Versi 2.0.3 yang Wajib Kalian Tahu
  • Performa Maksimal! Ini Cara Manual Update Ubuntu ke Linux Kernel 6.18 LTS
  • Inilah Cara Membuat AI Agent Cerdas dan Memantaunya Menggunakan LangGraph dan LangSmith
  • Ini Alasan China Akan Menang di Dunia AI
  • Apa Itu AI Gateway? Ini Definisi Tulang Punggung Infrastruktur AI Modern
  • Apa Itu Google AI Pro Ultra? Pengertian dan Bedah Fitur Terbarunya
  • Apa Itu FARA 7B? Ini Pengertian dan Cara Menjalankannya di Windows 11
  • Apa Itu Distribusi Malware Via YouTube? Ini Pengertian dan Bahayanya Buat Kalian
  • Apa Itu Undang-Undang NDAA? Ini Pengertian dan Dampaknya Bagi Cyber Command Amerika Serikat
  • Tagihan AWS Sekarang Bisa Dibayar Pakai BNB via BPN, Ini Caranya!
  • Video Botol Teh Pucuk 1 Menit 50 Detik yang Viral di TikTok! Hati-Hati Kejahatan Siber
  • Apa Itu Kerentanan WatchGuard CVE-2024-36193? Ini Penjelasannya
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

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