Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 39 / 68

Normalizing Strings (trim, Unifying Case)

In this lesson you will learn to normalize strings with trim and case folding so you can compare user input safely. This is for anyone searching for "PHP trim how to use".

Text entered by a user tends to carry stray leading or trailing spaces and inconsistent capitalisation. Stripping the spaces with trim() and unifying the case with strtolower() is called normalization.

The sample code combines both steps into one operation with strtolower(trim($str)) inside a normalize function. You can see that " Hello World " and "hello world", which look quite different, compare as the same string once normalized.

A common stumbling block for beginners is applying the normalization to only one side of the comparison. Whenever you write string-comparison code, make a point of applying the same normalization to both strings.

Skip this and two values that look identical to a human can still come back as false. It is a basic technique you cannot do without in input validation and login handling.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)