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

  • Apakah Aplikasi Labora Bisa Hasilkan Uang Jutaan atau Cuma Tipu-Tipu?
  • Apakah APK Cashcash Pro Penipu? Cek Reviewnya Dulu
  • Google Kliks Communicator: Pengalaman Awal yang Mengejutkan – Apa yang Kami Rasakan?
  • Belum Tahu? Ini Ukuran Banner YouTube yang Pas Biar Channel Kalian Makin Kece!
  • Inilah Rencana Gila China Balap AS ke Bulan Sebelum 2030
  • Inilah Spesifikasi Resmi Macbook Pro M5 yang Masuk Indonesia
  • Nonton Drama Bisa Dapat Cuan? Simak Cara Main Yudia APK Biar Nggak Salah Langkah!
  • Cuma Nonton Drama Pendek Bisa Cair Saldo DANA? Cobain Aplikasi Melolo, Begini Caranya!
  • Nonton Drama Bisa Dapat Cuan? Jangan Senang Dulu, Simak Fakta Mengejutkan Aplikasi Cash Drama Ini!
  • Ini Video Asli Bocil Block Blast 1 VS 3 di TikTok, Awas Jangan Asal Klik Link Sembarangan!
  • Instapop Terbukti Membayar atau Cuma Tipuan? Cek Faktanya Sebelum Kalian Buang Waktu Main Game!
  • Geger Video Botol Coca Cola 24 Detik di TikTok, Jangan Asal Klik! Ini Fakta Sebenarnya
  • Apa itu Video Botol Makarizo Viral? Ini Faktanya
  • Apa itu Merge Fruit Master? Ini Pengertian dan Cara Main Game yang Katanya Bisa Menghasilkan Saldo
  • Apa itu Shortbox? Ini Pengertian dan Cara Menghasilkan Uang di Aplikasinya
  • Masih Bingung Bedanya CRM dan ERP? Ini Tips Cari Software CRM dan ERP Terbaik
  • Begini Cara Hapus Akun DANA Premium Secara Permanen!
  • Bukan Cuma Mitos! Ternyata Dinasti Song Punya Tempat Nongkrong Mirip PIK, Begini Isinya
  • Bukan Fiksi! Ini Bukti Manusia Punya ‘Kekuatan Super’ Akibat Mutasi Genetik dan Lingkungan
  • Dikelilingi Cincin Api, Kok Kalimantan Malah Nggak Punya Gunung Berapi? Ini Jawabannya!
  • Misteri Newgrange: Kuburan Batu 5.000 Tahun yang Menyimpan Peta Galaksi, Kok Bisa?
  • Transformasi Ekstrem Pesawat IL-96: Ganti 4 Mesin Jadi 2 Mesin Raksasa PD-35, Emang Bisa?
  • Apakah Aplikasi Cash Reels Penipu?
  • Ini Daftar Kode Redeem Fish It Roblox Terbaru Januari 2026 buat Panen Hadiah!
  • Main Game Dapat Duit? Cek Dulu Fakta Shop Shelf Master Sebelum Nyesel!
  • Monitor Putih Sejutaan Spek Dewa, Acer S243y Bikin Ngiler!
  • Kapsul Luar Angkasa China Ini Bisa Mendarat Selembut Awan, SpaceX Bakal Ketar-ketir?
  • Kereta Maglev 500 KM/Jam: Proyek Gila 1.100 Triliun dari Jepang
  • Kenapa Perusahaan China Mulai Ungguli Amerika di Sektor AI dan Otomotif
  • Mau Beli iPhone 17 Tapi Budget Mepet? Ini Daftar Alternatif iPhone yang Masih Layak Dibeli
  • 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
  • Tutorial Optimasi LangGraph dengan Node-Level Caching untuk Performa Lebih Cepat
  • Tutorial Membuat Game Dengan LangChain
  • X Terancam Sanksi Eropa Gara-Gara AI Grok Bikin Deepfake Anak Kecil
  • Nano Banana 2 Flash Itu Apa Sih? Ini Penjelasan Lengkapnya
  • Inilah Cara Install dan Pakai ChatGPT Atlas Browser, Pesaing Baru di Dunia Web
  • Apa Itu Integrasi Criminal IP dan Cortex XSOAR? Ini Definisinya
  • Gawat! VVS Stealer Cuma Seharga Makan Siang Tapi Bisa Habisi Akun Discord Kalian
  • Jadi Subscriber OnlyFans? Ini Risiko Fatal yang Mengintai Kalian!
  • Apa Itu CVE-2025-14733? Ini Pengertian Vulnerability Kritis di WatchGuard Firebox
  • Apa itu CVE-2020-12812? Ini Pengertian dan Bahaya Bug 2FA Bypass di Fortinet
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