Join my Alpine.js course on Udemy for free 👀

String casing in PHP

December 21, 2021  ‐ 3 min read

Lowercase the whole string

In order to lowercase a PHP string you need the strtolower() function. It takes a string as a parameter and returns the lowercase result of the string passed as a parameter.

See the following example:

<?php

$nl = strtolower('The Netherlands');
echo $nl;
//-> the netherlands

The result depends on the locale that is set, in the default English locale that is set in "C" some special characters are not lowercased such as the "Ä".

See the docs: strtolower

Uppercase the whole string

To uppercase an entire string you need the PHP function strtoupper(). It takes a string as a parameter and returns the lowercase result of the string passed as a parameter.

See the following example:

<?php

$nl = strtoupper('The Netherlands');
echo $nl;
//-> THE NETHERLANDS

See the docs: strtoupper

Uppercase the first letter of a string

Besides functions that work on the entire string PHP provides functions that work on just a substring of the string that is passed as a parameter, one of them is the ucfirst() function (short for uppercase the first letter).

See the following example:

<?php

$nl = ucfirst('the netherlands');
echo $nl;
//-> The netherlands

You should keep in mind that this function only changes the case of the first letter in a string. So take a look at the following example:

<?php

$nl = ucfirst('THE NETHERLANDS');
echo $nl;
//-> THE NETHERLANDS

So in order to only have the first letter uppercased it might be preferable, depending on the strings you expect, to combine ucfirst() with strtolower() as the following example demonstrates:

<?php

$nl = ucfirst(strtolower('THE NETHERLANDS'));
echo $nl;
//-> The netherlands

See the docs: ucfirst

Uppercase the first letter of each word

Besides upper casing the first letter of a string you can uppercase the first character of each word in a string with ucwords().

<?php

$nl = ucwords('the netherlands');
echo $nl;
//-> The Netherlands

Just as with ucfirst() only the casing of the first letters are changed. Other letters that are already uppercased maintain their casing. So in order to only have the first letter uppercased it might be safer to combine ucwords() with strtolower().

<?php

$nl = ucwords(strtolower('THE NETHERLANDS'));
echo $nl;
//-> The Netherlands

What the ucwords function considers words is determined by the value of a second parameter to this function. This second parameter sets the characters that are considered word separators, meaning that the characters in a string after such a character are considered to be a different word.

The default value of this separator parameter is set to " \t\r\n\f\v", notice the space in there as well. The other values are in order the escape sequences of the: horizontal tab, carriage return, newline, form-feed and vertical tab.

We can overwrite this second parameter as follows:

<?php

$nl = ucwords('the-netherlands');
echo $nl;
//-> The-netherlands
$nl = ucwords('the-netherlands', '-');
echo $nl;
//-> The-Netherlands

See the docs: ucwords