What best practices should be followed when replacing multiple occurrences of a pattern in a string using preg_replace in PHP?

When replacing multiple occurrences of a pattern in a string using preg_replace in PHP, it is important to use the correct regular expression pattern and modifiers to ensure all instances are replaced. Additionally, using the preg_replace function with the correct parameters will help achieve the desired result without unexpected behavior.

// Example: Replace all occurrences of 'apple' with 'orange' in a string
$string = "I like apple, apple is my favorite fruit.";
$pattern = "/apple/";
$replacement = "orange";

$new_string = preg_replace($pattern, $replacement, $string);

echo $new_string;