How can the trim() function be utilized to address discrepancies in string comparisons in PHP scripts?

When comparing strings in PHP, discrepancies can occur due to whitespace characters such as spaces or tabs. This can lead to unexpected results when comparing strings that may appear identical but have trailing or leading whitespaces. By using the trim() function in PHP, we can remove any leading or trailing whitespaces from the strings before comparing them, ensuring accurate results.

$string1 = "   Hello";
$string2 = "Hello   ";

if(trim($string1) === trim($string2)) {
    echo "Strings are equal after trimming whitespace.";
} else {
    echo "Strings are not equal after trimming whitespace.";
}