Backend Development 5 min read

PHP strtolower() Function: Syntax, Parameters, Return Value, Examples, and Usage Tips

The article explains PHP's strtolower() function, detailing its syntax, parameters, return value, and providing multiple code examples—including full string conversion, partial conversion, word-level manipulation, and multibyte handling—while also noting important considerations for case sensitivity and non‑alphabetic characters.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP strtolower() Function: Syntax, Parameters, Return Value, Examples, and Usage Tips

In PHP, the strtolower() function converts all characters in a string to lowercase. It takes a string as input and returns the transformed string.

strtolower() Function Syntax

<code>strtolower(string $string)</code>

Parameter Description:

$string : Required. The string to be converted to lowercase.

Return Value:

Returns a new string where all characters of the original string have been converted to lowercase.

strtolower() Function Usage Example

<code>$string = "Hello World";<br/>$lowercaseString = strtolower($string);<br/>echo $lowercaseString; // Output: hello world</code>

In this example, strtolower() converts the string "Hello World" to lowercase and stores the result in $lowercaseString , which is then printed.

Important Notes

strtolower() is case‑sensitive and converts uppercase letters to their lowercase equivalents based on ASCII values; lowercase letters remain unchanged.

Non‑alphabetic characters such as numbers or punctuation are left untouched.

The function can handle multibyte characters (UTF‑8), making it useful for internationalized (i18n) and localized (l10n) text.

Different Uses of strtolower()

Example 1: Convert an entire string to lowercase

<code>$string = "Hello World";<br/>$lowercaseString = strtolower($string);<br/>echo $lowercaseString; // Output: hello world</code>

Example 2: Convert only a part of the string to lowercase

<code>$string = "Hello World";<br/>$lowercaseString = strtolower(substr($string, 0, 5));<br/>echo $lowercaseString; // Output: hello</code>

Example 3: Convert the first letter of each word to lowercase

<code>$string = "Hello World";<br/>$lowercaseString = strtolower(wordwrap($string, 5, " ", true));<br/>echo $lowercaseString; // Output: hello world</code>

Example 4: Convert all words in the string to lowercase using regex

<code>$string = "Hello World";<br/>$lowercaseString = preg_replace('/\b\w+\b/', '${0,}', $string); // Use regex to convert words to lowercase<br/>echo $lowercaseString; // Output: hello world (same as strtolower)</code>

Example 5: Handle multibyte characters (e.g., UTF‑8 strings)

<code>$string = "你好世界"; // UTF‑8 string containing Chinese characters<br/>$lowercaseString = strtolower($string); // Convert Chinese characters to lowercase (no change)
echo $lowercaseString; // Output: 你好世界</code>

Summary

The strtolower() function in PHP is useful for converting all characters in a string to lowercase, which is valuable for case‑insensitive text processing, handling filenames and URLs, and database queries. It also supports multibyte characters, making it suitable for internationalized applications.

PHP Learning Recommendations

Vue3+Laravel8+Uniapp Beginner to Real‑World Development Tutorial

Vue3+TP6+API Social E‑Commerce System Development Course

Swoole From Beginner to Advanced Recommended Courses

Workerman+TP6 Real‑Time Chat System Limited‑Time Offer

ProgrammingPHPstring-functionsstrtolower
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.