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? Inilah Cara Upload Reels Instagram Sampai 20 Menit, Konten Jadi Lebih Puas!
  • Apa itu Negara Somaliland? Apa Hubungannya dengan Israel?
  • Apa itu Game TheoTown? Game Simulasi Jadi Diktator
  • Inilah Rekomendasi 4 HP Honor Terbaik – Prosesor Snapdragon Tahun 2026
  • Lagi Nyari HP Gaming Murah? Inilah 4 HP Asus RAM 8 GB yang Recomended
  • Ini Trik Main Game Merge Cats Road Trip Sampai Tarik Saldo ke DANA
  • Mau Jadi Digital Writer Pro? Ini Caranya Buat Portofolio Pakai Blog!
  • Ini Cara Login Banyak Akun FB & IG di Satu HP Tanpa Diblokir!
  • Inilah Cara Mengatasi Verval Siswa Silang Merah di RDM versi Hosting
  • HP Tertinggal? Inilah Caranya Login PDUM Langsung dari Laptop, Lebih Praktis!
  • Inilah Cara Tarik Dana dari APK Drama Rush
  • Inilah Cara Mudah Tarik Uang Kertas Biru di Merge Cats ke DANA dan OVO Tanpa Ribet!
  • Apakah Aplikasi Pinjaman KlikKami Penipu? Ada DC Penagih?
  • Kenapa Tentara Romawi Hanya Pakai Armor Kaki Saja?
  • Inilah Alasan Kenapa Beli Follower IG itu TIDAK AMAN!
  • EPIK! Kisah Mesin Bor Tercanggih Takluk di Proyek Terowongan Zojila Himalaya
  • Bingung Cari Lokasi Seseorang? Cek Cara Melacak Pemilik Nomor HP Tanpa Bayar Ini, Dijamin Akurat!
  • Apa itu Logis? Kenapa Logika Bisa Berbeda-beda?
  • Ini Alasan Kenapa Fitur Bing AI Sedang Trending dan Dicari Banyak Orang
  • Sejarah Kerajaan Champa: Bangsa Yang Hilang Tanpa Perang Besar, Kok Bisa?
  • Gini Caranya Dapat Weekly Diamond Pass Gratis di Event M7 Pesta, Ternyata Nggak Pake Modal!
  • Inilah Trik Rahasia Panen Token dan Skin Gratis di Event Pesta Cuan M7 Mobile Legends!
  • Apakah Apk Pinjaman Cepat Galaxy Pinjol Penipu?
  • Cara Tarik Saldo APK Game Clear Blast
  • Apakah APK Game Clear Blast Penipu? Ini Reviewnya
  • Inilah Perbedaan SEO dan GEO + Tips Konten Disukai Google dan AI!
  • Inilah Cara Download Video TikTok 2026 Tanpa Watermark
  • Belum Tahu? Ini Trik Nonton Doods Pro Bebas Iklan dan Cara Downloadnya
  • Misteri DNA Spanyol Terungkap: Jauh Lebih Tua dari Romawi dan Moor!
  • Kenapa Belut Listrik itu Sangat Mematikan
  • Tailwind’s Revenue Down 80%: Is AI Killing Open Source?
  • Building Open Cloud with Apache CloudStack
  • TOP 1% AI Coding: 5 Practical Techniques to Code Like a Pro
  • Why Your Self-Hosted n8n Instance Might Be a Ticking Time Bomb
  • CES 2026: Real Botics Wants to Be Your Best Friend, but at $95k, Are They Worth the Hype?
  • Inilah Cara Belajar Cepat Model Context Protocol (MCP) Lewat 7 Proyek Open Source Terbaik
  • Inilah Cara Menguasai Tracing dan Evaluasi Aplikasi LLM Menggunakan LangSmith
  • Begini Cara Menggabungkan LLM, RAG, dan AI Agent untuk Membuat Sistem Cerdas
  • Cara Buat Sistem Moderasi Konten Cerdas dengan GPT-OSS-Safeguard
  • Inilah Cara Membuat Aplikasi Web Full-Stack Tanpa Coding dengan Manus 1.5
  • Apa itu CVE-2025-14847? Ini Penjelasan Lengkap MongoBleed
  • Ini Kronologi & Resiko Kebocoran Data WIRED
  • Apa itu Grubhub Crypto Scam? Ini Pengertian dan Kronologi Penipuan yang Catut Nama Grubhub
  • Apa Itu CVE-2025-59374? Mengenal Celah Keamanan ASUS Live Update yang Viral Lagi
  • Apa itu RansomHouse Mario? Ini Pengertian dan Mengenal Versi Baru ‘Mario’ yang Makin Bahaya
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