Converting a string to lower case

PHP:
Converting a string to lower case

How to:

PHP uses strtolower to make all characters in a string lower case. Here’s how it works:

<?php
$originalString = "HeLLo WoRLD!";
$lowerCaseString = strtolower($originalString);

echo $lowerCaseString; // Outputs: hello world!
?>

If you need to handle multibyte character encodings, like UTF-8, use mb_strtolower instead:

<?php
$originalString = "İstanbul";
$lowerCaseString = mb_strtolower($originalString, 'UTF-8');

echo $lowerCaseString; // Outputs: istanbul (correctly converts İ to i)
?>

Deep Dive

Historically, PHP’s strtolower function has been the go-to function for case conversion, introduced in very early versions of PHP. However, as PHP applications became more global, the need to correctly handle multibyte character encodings brought about mb_strtolower.

Alternatives to strtolower and mb_strtolower include using regular expressions with the mb_ereg_replace_callback function or preg_replace_callback, but for simple case conversion, they are overkill.

In PHP, strings have traditionally been byte-based, not character-based, meaning each byte is one character. This works for single-byte encodings like ASCII, where each character indeed is one byte. For multibyte encodings, mb_strtolower understands character encoding and treats characters as they should be treated.

See Also