How can PHP developers prevent duplicate user names with variations in case and spacing during registration and login?

To prevent duplicate user names with variations in case and spacing during registration and login, PHP developers can convert all usernames to lowercase before storing them in the database. This ensures that regardless of the case or spacing used during registration, the username will be standardized for comparison purposes during login.

// Convert username to lowercase before storing in the database
$username = strtolower($_POST['username']);

// Check if the username already exists in the database
$query = "SELECT * FROM users WHERE LOWER(username) = '$username'";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) > 0) {
    // Username already exists
    // Handle the error accordingly
} else {
    // Proceed with user registration
}