How does the preg_match function work in PHP and what is its relationship with trim()?

The preg_match function in PHP is used to perform a regular expression match on a string. It searches a string for a pattern and returns true if the pattern is found, otherwise it returns false. The trim() function, on the other hand, is used to remove whitespace or other specified characters from the beginning and end of a string. The two functions are unrelated in terms of functionality, but they can be used together to manipulate and validate strings in PHP.

$string = "  Hello, World!  ";
$pattern = '/^Hello/';
if (preg_match($pattern, trim($string))) {
    echo "String starts with 'Hello'";
} else {
    echo "String does not start with 'Hello'";
}