How can the trim() function be used to improve the accuracy of data comparison in PHP scripts?

When comparing data in PHP scripts, extra whitespace at the beginning or end of strings can lead to inaccurate results. By using the trim() function, we can remove any leading or trailing whitespace from strings before comparing them, ensuring that the comparison is based solely on the content of the strings.

$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";
}