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 Cara Mengaktifkan Fitur Vertical Tab di Google Chrome Biar Tampilan Browser Kalian Makin Lega dan Rapi
  • Inilah Alasan Kenapa elementary OS Adalah Pilihan Paling Pas Buat Pengguna macOS yang Ingin Coba Linux
  • Inilah Alasan Kenapa Windows Server Kalian Restart Terus Setelah Update April 2026
  • Inilah Alasan Layar TV Kalian Sering Kedip Hitam Saat Main Game dan Cara Mengatasinya
  • Inilah Rekomendasi HP Redmi Note RAM 12 GB dan Koneksi 5G Paling Worth It Buat Kalian
  • Inilah Bocoran Event FF Mei 2026 yang Siap Bikin Dompet Kering, Ada Kuda dan MP40 Cobra!
  • Inilah Kejutan Kompensasi Bug FC Mobile 18 April 2026, Ada Kartu Flashback Gratis yang Bikin Skuad Makin Gahar!
  • Inilah Greenwashing: Trik Nakal Perusahaan Biar Terlihat Ramah Lingkungan Padahal Enggak!
  • Inilah Rekomendasi Parfum Segar untuk Cuaca Panas Agar Kalian Tetap Wangi Seharian Tanpa Bikin Pusing
  • Inilah 12 Aplikasi TV Online Terbaik 2026, Nonton Siaran Langsung Jadi Makin Gampang!
  • Inilah Rekomendasi Kipas Angin Dinding Terbaik Buat Ngatasin Cuaca Panas Tanpa Bikin Kantong Jebol
  • Inilah Cara Sewa Mobil yang Aman dan Nyaman Biar Liburan Kalian Makin Seru Tanpa Drama
  • Inilah Cara Daftar Lowongan 30.000 Manajer Koperasi Desa Merah Putih, Cek Jadwal Lengkapnya Di Sini!
  • Inilah Cara Daftar Lowongan 1.369 Manajer Operasional Kampung Nelayan Merah Putih Jadi Pegawai BUMN!
  • Inilah Perbedaan Skin Tint, Cushion, dan Foundation Biar Kalian Nggak Salah Pilih Base Makeup Natural
  • Inilah Rekomendasi Sepeda Lipat Dewasa Terbaik 2026 yang Paling Praktis untuk Kebutuhan Commuting dan Olahraga Harian
  • Inilah 5 Rekomendasi Sepeda Lipat Listrik Jarak Tempuh Terjauh Biar Mobilitas Kalian Makin Praktis!
  • Inilah 30 Ucapan Hari Kartini 2026 Paling Bermakna untuk Caption Media Sosial dan Pesan Personal yang Inspiratif
  • Inilah Cara Daftar Manajer Koperasi Merah Putih, Peluang Karir BUMN dengan 30.000 Formasi
  • Inilah 5 Rekomendasi Bedak Two Way Cake Anti Oksidasi Supaya Wajah Nggak Terlihat Kusam dan Gelap
  • Inilah Syarat dan Cara Mengurus Surat Sehat Jasmani Rohani untuk Daftar Manajer Kopdes Merah Putih
  • Inilah Alasan Harga Plastik Naik dan 7 Alternatif Kemasan Ramah Lingkungan yang Lebih Hemat buat UMKM
  • Inilah Profil Syekh Ahmad Al Misry dan Rekam Jejaknya yang Sedang Viral di Media Sosial
  • Inilah Alasan HP Nokia Jadul Harganya Masih Selangit dan Banyak Diburu Kolektor di Tahun 2026
  • Inilah HP Samsung A Series Terbaru yang Pakai Layar AMOLED, Mewah Tapi Harganya Terjangkau Banget!
  • Inilah Vivo T5 Pro, HP Midrange dengan Baterai 9.020 mAh yang Siap Comeback ke Indonesia!
  • Inilah Cara Daftar Manajer Koperasi Desa Merah Putih 2026, Ada 30.000 Formasi yang Bisa Kalian Lamar!
  • Inilah Alasan Kenapa Kemkomdigi Ancam Blokir Wikipedia dan Dampak Seriusnya Buat Masa Depan Pendidikan Kita
  • Inilah Cara Cek BLT Desa 2026 Tahap 2, Cair Rp900 Ribu Langsung ke Tangan Kalian!
  • Inilah Cara Cek PIP Lewat HP 2026 dan Panduan Lengkap Pencairannya Biar Dana Nggak Hangus
  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to use the Wan 2.7 AI model: A complete walkthrough
  • How to Run Minimax M2-7 Locally: A Step-by-Step Guide for Beginners
  • How to Build Self-Driving Codebases with Async Agents and NVIDIA Hardware
  • How to create a professional AI influencer using Apob AI
  • How to Use AI-Powered Tools to Level Up Your Stock Market Research Game with Claude Code
  • CrownPlay Konto‑Verifizierung – Schritt‑für‑Schritt Anleitung für deutsche Spieler
  • Coronavirus disease 2019
  • La modernidad de Chamartín, distrito financiero de Madrid.
  • New Text Document
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin

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