What is the purpose of the preg_match function in PHP and how does it work?

The preg_match function in PHP is used to perform a regular expression match on a string. It is commonly used to check if a specific pattern exists in a given string. The function returns 1 if the pattern is found in the string, and 0 if not found.

<?php
$string = "Hello, World!";
$pattern = "/Hello/";
if (preg_match($pattern, $string)) {
    echo "Pattern found in the string.";
} else {
    echo "Pattern not found in the string.";
}
?>