#35: Making a String Uppercase, Lowercase, or Capitalized

One occasional problem with PHP is that MySQL supports case-insensitive character fields, but strings in PHP are case sensitive. In a query, MySQL makes no distinction between the words Ferrett, FERRETT, and FerReTt, but as strings in PHP, they have nothing in common. So in PHP you may need to change the case of characters in a string before you compare or print them.

PHP has three essential functions that convert string case: strtolower(), strtoupper(), and ucwords(). Here is a sample of them in action:

<? $string = "heY hOw arE yoU doinG?"; echo strtoupper($string); // Displays "HEY HOW ARE YOU DOING?" echo strtolower($string); // Displays "hey how are you doing?" echo ucwords(strtolower($string)); ...

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.