In PHP, strtolower() is a built-in function that converts a string to lowercase. It's useful when you want to ensure consistency, especially in cases like comparing strings (e.g., user inputs, email addresses, etc.) where letter casing might differ.
Syntax:
strtolower(string $string): string
- Parameter: A string you want to convert to lowercase.
- Returns: The lowercase version of the string.
Example 1: Basic usage
$text = "HeLLo WoRLD!";
$lowercaseText = strtolower($text);
echo $lowercaseText;
// Output: hello world!
Example 2: Email normalization
$userEmail = "John.DOE@Example.com";
$normalizedEmail = strtolower($userEmail);
echo $normalizedEmail;
// Output: john.doe@example.com
Example 3: Case-insensitive comparison
$userInput = "Admin";
$expectedRole = "admin";
if (strtolower($userInput) === $expectedRole) {
echo "Access granted.";
} else {
echo "Access denied.";
}
// Output: Access granted.
Notes: