What is the purpose of str_replace() in PHP and when should it be used?

The str_replace() function in PHP is used to replace all occurrences of a search string with a replacement string within a given string. It is commonly used to manipulate text data, such as replacing certain characters or words with others. This function can be particularly useful when you need to modify or clean up user input, format data, or make specific changes to a string.

// Example of using str_replace() to replace 'apple' with 'orange' in a string
$string = "I like apple pie and apple juice.";
$new_string = str_replace('apple', 'orange', $string);
echo $new_string;