What is the purpose of using trim() function in PHP when checking for empty variables?

When checking for empty variables in PHP, it is important to use the trim() function to remove any leading or trailing whitespace that may be present in the variable. This ensures that the variable is truly empty if it only contains whitespace characters, and helps prevent false positives when checking for emptiness.

// Example of using trim() function when checking for empty variables
$var1 = "   "; // variable with only whitespace
$var2 = "  test  "; // variable with whitespace and text

if (empty(trim($var1))) {
    echo "Variable 1 is empty.";
} else {
    echo "Variable 1 is not empty.";
}

if (empty(trim($var2))) {
    echo "Variable 2 is empty.";
} else {
    echo "Variable 2 is not empty.";
}