How does the output of preg_replace differ from preg_match when using regular expressions in PHP?

When using regular expressions in PHP, preg_match is used to search a string for a match to a given pattern, returning true if a match is found and false otherwise. On the other hand, preg_replace is used to search a string for a pattern and replace it with a specified replacement. The output of preg_match is a boolean value indicating whether a match was found, while the output of preg_replace is the modified string with replacements made.

// Example of using preg_match
$string = "Hello, World!";
$pattern = "/Hello/";
if (preg_match($pattern, $string)) {
    echo "Match found!";
} else {
    echo "No match found.";
}

// Example of using preg_replace
$string = "Hello, World!";
$pattern = "/Hello/";
$replacement = "Hi";
$new_string = preg_replace($pattern, $replacement, $string);
echo $new_string;